所有id
列都有 auto_increment
在我的触发器中:
ALTER trigger [dbo].[mytrig]
on [dbo].[requests]
after INSERT, UPDATE
as
begin
declare @MyId1 int
set @MyId1 = (select Id from inserted)
declare @MyId2 int
declare @MyId3 int
if (select column1 from inserted) = 1
begin
insert into [dbo].[contracts] select column1,column2,column3 .... from inserted
set @MyId2 = SCOPE_IDENTITY()
insert into [dbo].[History] select column1,column2,column3 .... from inserted
set @MyId3 = SCOPE_IDENTITY()
insert into [dbo].[contracts_depts](Id_Contract ,column5) select @MyId2,column6 from request_depts where Id_request=@MyId1
insert into [dbo].[History_depts] (Id_InHistory,column5) select @MyId3,column6 from request_depts where Id_request=@MyId1
end
end
@MyId1
仅在更新后而不是在插入后返回值。我必须使用scope_identity()
还是什么?