我有此代码:
CREATE TABLE [dbo].[test1]
(
[Id] [BIGINT] IDENTITY(1,1) NOT NULL,
[i] [BIGINT] NULL,
[j] [NVARCHAR](50) NULL
)
CREATE TRIGGER [dbo].[test1_ioi]
ON [dbo].[test1]
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Ids TABLE (Id BIGINT);
INSERT INTO test1
OUTPUT INSERTED.id --[state2] INTO @Ids(ID)
SELECT i+1, j+'--'
FROM inserted;
--[state2] select * from @Ids;
END
当我运行此查询时:
DECLARE @Snd_Ids TABLE (Id BIGINT);
INSERT INTO [dbo].[test1] ([i], [j])
OUTPUT INSERTED.ID INTO @Snd_Ids(ID)
VALUES (1, 2), (3, 4), (5, 6);
SELECT * FROM @Snd_Ids;
结果是:0,0,0。
未显示触发器的输出。为什么?
如果我删除评论:--[state2]
我可以看到两个结果,但是0, 0, 0
对我无效!
有人可以帮助我吗?
我想从INSTEAD OF INSERT
获取结果或输出,并将其用于其他SQL语句(在同一会话中)。