SQL触发器+游标
1 Create trigger tri_wk_CSVHead_History on wk_CSVHead_History 2 --声明一个tri_wk_CSVHead_History触发器,
3 instead of insert ---插入操作被代替执行下面的操作 4 as 5 begin 6 declare YB cursor --声明一个游标 7 for 8 select NoteNO from inserted--这里的NoteNO要和下面的匹配 9 10 open YB 11 declare @NoteNO varchar(50)--这里的NoteNO要和上面的匹配,定义一个游标变量@NoteNO,用来操作insered表中的NoteNo. 12 13 14 fetch next from YB into @NoteNO--这里的NoteNO要是上面的匹配,移动游标 15 16 while(@@fetch_status=0)--0操作成功,-1 FETCH 语句失败或此行不在结果集中,-2 被提取的行不存在 17 begin 18 delete from wk_CSVDetail_History where NoteNO=@NoteNO 19 delete from wk_CSVHead_History where NoteNO=@NoteNO 20 fetch next from YB into @NoteNO --继续移动游标,直到@@fetch_status不等于时. 21 end 22 close YB --关闭游标 23 deallocate YB --释放游标 24 insert into wk_CSVHead_History select * from inserted 25 26 end