Using OUTPUT/INTO within instead of insert trigger invalidates 'inserted' table

核能气质少年 提交于 2019-12-06 01:50:57

问题


I have a problem using a table with an instead of insert trigger.

The table I created contains an identity column. I need to use an instead of insert trigger on this table. I also need to see the value of the newly inserted identity from within my trigger which requires the use of OUTPUT/INTO within the trigger. The problem is then that clients that perform INSERTs cannot see the inserted values.

For example, I create a simple table:

CREATE TABLE [MyTable](
 [MyID] [int] IDENTITY(1,1) NOT NULL,
 [MyBit] [bit] NOT NULL,
 CONSTRAINT [PK_MyTable_MyID] PRIMARY KEY NONCLUSTERED 
(
 [MyID] ASC
))

Next I create a simple instead of trigger:

create trigger [trMyTableInsert] on [MyTable] instead of insert
as
BEGIN
 DECLARE @InsertedRows table( MyID int,
                              MyBit bit);

 INSERT INTO [MyTable]
      ([MyBit])
   OUTPUT inserted.MyID,
          inserted.MyBit
   INTO @InsertedRows
   SELECT inserted.MyBit
     FROM inserted;

 -- LOGIC NOT SHOWN HERE THAT USES @InsertedRows
END;

Lastly, I attempt to perform an insert and retrieve the inserted values:

DECLARE @tbl TABLE (myID INT) 

insert into MyTable 
 (MyBit)
OUTPUT inserted.MyID
  INTO @tbl
 VALUES (1)

 SELECT * from @tbl

The issue is all I ever get back is zero. I can see the row was correctly inserted into the table. I also know that if I remove the OUTPUT/INTO from within the trigger this problem goes away.

Any thoughts as to what I'm doing wrong? Or is how I want to do things not feasible?

Thanks.


回答1:


You haven't specified what your 'clients' are, but if they are a .Net or similar app, you simply need to drop the INTO portion of your output. Your clients can't see the results because they are not being returned in the result set.

Your trigger then should look like this:

create trigger [trMyTableInsert] on [MyTable] instead of insert
as
BEGIN

 INSERT INTO [MyTable]
      ([MyBit])
   OUTPUT inserted.MyID,
          inserted.MyBit
   SELECT inserted.MyBit
     FROM inserted;

 -- LOGIC NOT SHOWN HERE THAT USES @InsertedRows
END;

This causes inserts to behave like a select query as far as the client is concerned - you can iterate over the results to get the identity value (or other special values like Timestamp).

This is how LinqToSql supports identity columns with instead of insert triggers.




回答2:


the culprit here is actually the identity column; if you replace it with a sequence then everything just works™

create sequence testing_id as int start with 1 increment by 1

create table testing (id int default (next value for testing_id), s varchar(100))

create trigger tr_testing on testing instead of insert as
begin
    declare @inserted table (id int, s varchar(100))
    insert into testing
    output inserted.* into @inserted
    select * from inserted
end

declare @inserted table (id int, s varchar(100))
insert into testing (s)
output inserted.* into @inserted
values ('testing1'),('testing2')

select * from @inserted


来源:https://stackoverflow.com/questions/2482025/using-output-into-within-instead-of-insert-trigger-invalidates-inserted-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!