在触发器中使用 OUTPUT/INTO 而不是插入触发器会使“插入”无效。桌子

发布于 2024-08-25 16:25:16 字数 1059 浏览 2 评论 0原文

我在使用带有替代插入触发器的表时遇到问题。

我创建的表包含一个标识列。我需要在此表上使用而不是插入触发器。我还需要从触发器中查看新插入的标识的值,这需要在触发器中使用 OUTPUT/INTO。问题是执行 INSERT 的客户端无法看到插入的值。

例如,我创建一个简单的表:

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

接下来我创建一个简单的而不是触发器:

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;

最后,我尝试执行插入并检索插入的值:

DECLARE @tbl TABLE (myID INT) 

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

 SELECT * from @tbl

问题是我得到的只是零。我可以看到该行已正确插入到表中。我还知道,如果我从触发器中删除输出/输入,这个问题就会消失。

对我做错了什么有什么想法吗?还是我想做的事情不可行?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撞了怀 2024-09-01 16:25:16

您尚未指定您的“客户端”是什么,但如果它们是 .Net 或类似应用程序,您只需删除输出的 INTO 部分即可。您的客户端无法看到结果,因为它们没有在结果集中返回。

然后,您的触发器应如下所示:

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;

就客户端而言,这会导致插入的行为类似于选择查询 - 您可以迭代结果以获取标识值(或其他特殊值,如时间戳)。

这就是 LinqToSql 使用而不是插入触发器支持标识列的方式。

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.

皇甫轩 2024-09-01 16:25:16

来自 OUTPUT 子句 (Transact-SQL)

从 OUTPUT 返回的列反映了 INSERT、UPDATE 或 DELETE 语句完成之后、触发器执行之前的数据。

对于 INSTEAD OF 触发器,即使触发器操作的结果没有发生任何修改,也会生成返回的结果,就像实际发生了 INSERT、UPDATE 或 DELETE 一样。

..所以触发器不可能向输出结果添加任何内容(并且在替代的情况下,即使触发器没有插入任何内容,您也可能会得到输出!)

在您的情况下,因为您需要的只是插入的 id您可以使用序列,例如:

create sequence MySequence as int start with 1

create table MyTable (
    MyId int default (next value for MySequence),
    MyBit bit not null)

create trigger TR_MyTable on MyTable instead of insert as
begin
    insert into MyTable select * from inserted
    ...do stuff...
end

declare @tbl table (MyId int)
insert into MyTable output inserted.MyId into @tbl
values (1)

select * from @tbl
> 1, 1

TAKE CARE:您将返回为 MyBit 指定的任何值,即使触发器在插入之前更改它!

from OUTPUT Clause (Transact-SQL)

Columns returned from OUTPUT reflect the data as it is after the INSERT, UPDATE, or DELETE statement has completed but before triggers are executed.

For INSTEAD OF triggers, the returned results are generated as if the INSERT, UPDATE, or DELETE had actually occurred, even if no modifications take place as the result of the trigger operation.

..so it's impossible for the trigger to add anything to the output results (and in the case of instead-of you may get output even if the trigger didn't insert anything!)

in your case since all you need is the inserted id you could use a sequence, eg:

create sequence MySequence as int start with 1

create table MyTable (
    MyId int default (next value for MySequence),
    MyBit bit not null)

create trigger TR_MyTable on MyTable instead of insert as
begin
    insert into MyTable select * from inserted
    ...do stuff...
end

declare @tbl table (MyId int)
insert into MyTable output inserted.MyId into @tbl
values (1)

select * from @tbl
> 1, 1

TAKE CARE: you'll get back whatever value you specified for MyBit even if the trigger changes it before inserting!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文