SQL Server trigger + cursor operation implementation

Source: Internet
Author: User

Copy codeThe Code is as follows: Create trigger tri_wk_CSVHead_History on wk_CSVHead_History
-- Declare a tri_wk_CSVHead_History trigger,
Instead of insert --- the insert operation is replaced by the following Operation
As
Begin
Declare YB cursor -- declare a cursor
For
Select NoteNO from inserted -- The NoteNO here must match the following
Open YB
Declare @ NoteNO varchar (50) -- NoteNO must match the above and define a cursor variable @ NoteNO to operate NoteNo in the insered table.
Fetch next from YB into @ NoteNO -- If NoteNO matches the above, move the cursor.
While (@ fetch_status = 0) -- The 0 operation is successful. The-1 FETCH statement fails or the row is not in the result set. The-2 extracted row does not exist.
Begin
Delete from wk_CSVDetail_History where NoteNO = @ NoteNO
Delete from wk_CSVHead_History where NoteNO = @ NoteNO
Fetch next from YB into @ NoteNO -- move the cursor until @ fetch_status is not equal.
End
Close YB -- close the cursor
Deallocate YB -- release cursor
Insert into wk_CSVHead_History select * from inserted
End
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.