在触发器中使用 OUTPUT/INTO 而不是插入触发器会使“插入”无效。桌子
我在使用带有替代插入触发器的表时遇到问题。
我创建的表包含一个标识列。我需要在此表上使用而不是插入触发器。我还需要从触发器中查看新插入的标识的值,这需要在触发器中使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您尚未指定您的“客户端”是什么,但如果它们是 .Net 或类似应用程序,您只需删除输出的 INTO 部分即可。您的客户端无法看到结果,因为它们没有在结果集中返回。
然后,您的触发器应如下所示:
就客户端而言,这会导致插入的行为类似于选择查询 - 您可以迭代结果以获取标识值(或其他特殊值,如时间戳)。
这就是 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:
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.
来自 OUTPUT 子句 (Transact-SQL)
..所以触发器不可能向输出结果添加任何内容(并且在替代的情况下,即使触发器没有插入任何内容,您也可能会得到输出!)
在您的情况下,因为您需要的只是插入的 id您可以使用序列,例如:
TAKE CARE:您将返回为
MyBit
指定的任何值,即使触发器在插入之前更改它!from OUTPUT Clause (Transact-SQL)
..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:
TAKE CARE: you'll get back whatever value you specified for
MyBit
even if the trigger changes it before inserting!