--建立測試環境
Create Table A(ID Int Identity(1,1), Name Varchar(10))
Create Table B(ID Int)
GO
--建立觸發器
Create Trigger Update_B On A
For Insert
As
Begin
Insert B Select ID From A
End
GO
--測試
Insert A Select 'A'
Union All Select 'B'Select * From B
GO
--刪除測試環境
Drop Table A, B
--結果
/*
ID
1
2
*/
Create Table A(ID Int Identity(1,1), Name Varchar(10))
Create Table B(ID Int)
GO
--建立觸發器
Create Trigger Update_B On A
For Insert
As
Begin
Insert B Select ID From A
End
GO
--測試
Insert A Select 'A'
Union All Select 'B'Select * From B
GO
--刪除測試環境
Drop Table A, B
--結果
/*
ID
1
2
*/
Create Table A(ID Int Identity(1,1), Name Varchar(10))
Create Table B(ID Int)
GOcreate proc InsertTableA
(
@Name varchar(10)
)
as
begin trandeclare @ID int
insert into A(Name) select @Name
set @ID=@@Identity
if @@Error<>0
goto Flaginsert into B(ID) select @ID
if @@Error<>0
goto Flagcommit Tran
returnFlag:
rollback Tran
return
go
create table b(id int)create trigger inserttob on a
for insert
as
begin
insert into b select id from inserted
endinsert into a(name) select 'aaa'select * from a
select * from bid name
----------- ----------
1 aaa(所影响的行数为 1 行)id
-----------
1(所影响的行数为 1 行)