復制代碼 代碼如下:
Create trigger tri_wk_CSVHead_History on wk_CSVHead_History
--聲明一個tri_wk_CSVHead_History觸發器,
instead of insert ---插入操作被代替執行下面的操作
as
begin
declare YB cursor --聲明一個游標
for
select NoteNO from inserted--這裡的NoteNO要和下面的匹配
open YB
declare @NoteNO varchar(50)--這裡的NoteNO要和上面的匹配,定義一個游標變量@NoteNO,用來操作insered表中的NoteNo.
fetch next from YB into @NoteNO--這裡的NoteNO要是上面的匹配,移動游標
while(@@fetch_status=0)--0操作成功,-1 FETCH 語句失敗或此行不在結果集中,-2 被提取的行不存在
begin
delete from wk_CSVDetail_History where NoteNO=@NoteNO
delete from wk_CSVHead_History where NoteNO=@NoteNO
fetch next from YB into @NoteNO --繼續移動游標,直到@@fetch_status不等於時.
end
close YB --關閉游標
deallocate YB --釋放游標
insert into wk_CSVHead_History select * from inserted
end