我正在处理的数据库具有以下插入触发器。问题是触发器不适用于多行插入。当我尝试一次插入多行时,我收到以下消息。
Msg 512,Level 16,State 1,Procedure _ti_UD06,Line 8
子查询返回的值超过1。子查询时不允许这样做 follow =,!=,<,< =,>,> =或者当子查询用作 表达。我认为问题在于以下声明 插入多行时将返回多个值。
如何使此插入触发器可以使用多行?
IF(SELECT PROGRESS_RECID FROM inserted) IS NULL
表格中有2列,PROGRESS_RECID
和PROGRESS_RECID_IDENT_
。
我认为PROGRESS_RECID_IDENT_
是SQL Server的Identity
列。 PROGRESS服务器使用PROGRESS_RECID_INDENT
。
如果插入新记录,触发器会检查PROGRESS_RECID
是否为空,并将其设置为inserted.Identity
ALTER trigger [dbo].[_ti_UD06] ON [dbo].[UD06] for insert as
begin
if ( select PROGRESS_RECID from inserted) is NULL
begin
update t set PROGRESS_RECID = i.IDENTITYCOL
from UD06 t JOIN INSERTED i ON
t.PROGRESS_RECID_IDENT_ = i.PROGRESS_RECID_IDENT_
select convert (bigint, @@identity)
end
end
这是表定义
CREATE TABLE [dbo].[UD06](
[Company] [nvarchar](8) NULL,
[Key1] [nvarchar](50) NOT NULL,
[Key2] [nvarchar](50) NULL,
[Key3] [nvarchar](50) NULL,
[Key5] [nvarchar](50) NULL,
[Character01] [nvarchar](max) NULL,
[Character02] [nvarchar](max) NULL,
[Character03] [nvarchar](max) NULL,
[Character04] [nvarchar](max) NULL,
[Character05] [nvarchar](max) NULL,
[Character06] [nvarchar](max) NULL,
[Character07] [nvarchar](max) NULL,
[Character08] [nvarchar](max) NULL,
[Character09] [nvarchar](max) NULL,
[Character10] [nvarchar](max) NULL,
[SysRowID] [nvarchar](36) NULL,
[SysRevID] [int] NULL,
[BitFlag] [int] NULL,
[GlobalUD06] [tinyint] NULL,
[GlobalLock] [tinyint] NULL,
[PROGRESS_RECID] [bigint] NULL,
[PROGRESS_RECID_IDENT_] [bigint] IDENTITY(1,1) NOT NULL
) ON [PRIMARY]
答案 0 :(得分:1)
我认为你已经非常接近了:
ALTER trigger [dbo].[_ti_UD06] ON [dbo].[UD06] for insert as
begin
UPDATE t
SET PROGRESS_RECID = t.PROGRESS_RECID_IDENT_ --not "IDENTITYCOL"
FROM UD06 t
JOIN inserted i ON t.PROGRESS_RECID_IDENT_ = i.PROGRESS_RECID_IDENT_
WHERE i.PROGRESS_RECID is Null
-- Not sure what this bit is for? I doubt it's a good idea though...
select convert (bigint, @@identity)
end