ALTER TRIGGER t1
ON dbo.Customers
FOR INSERT
AS
BEGIN TRANSACTION
/* variables */
DECLARE
@maxid bigint
SELECT @customerid = id FROM inserted
SET IDENTITY_INSERT dbo.new_table ON
DECLARE
@maxid bigint
SELECT @maxid = MAX(ID) FROM new_table
INSERT INTO new_table (ID, ParentID, Foo, Bar, Buzz)
SELECT ID+@maxid, ParentID+@maxid, Foo, Bar, Buzz FROM initial_table
SET IDENTITY_INSERT dbo.new_tableOFF
/* execute */
COMMIT TRANSACTION
GO
失败:
SQL Server 子查询返回超过 1 个值。当子查询跟随 =、!=、<、<=、>、>= 或子查询用作表达式时,这是不允许的
如何解决?
我想做的是
- 插入
id
和parentid
,每个增加@maxid
- 从
initial_table
- 进入
new_table
谢谢
新表
id (bigint)
parentid (bigint - linked to id)
foo | bar | buzz (others are nvarchar, not really important)
初始表
id (bigint)
parentid (bigint - linked to id)
foo | bar | buzz (others are nvarchar, not really important)