Trigger + cursor operation implementation _MSSQL in SQL Server

Source: Internet
Author: User

Copy Code code as follows:

Create Trigger Tri_wk_csvhead_history on Wk_csvhead_history
--Declares a tri_wk_csvhead_history trigger,
The instead of INSERT---insert operation is substituted for the following action
As
Begin
DECLARE YB cursor--Declares a cursor
For
Select Noteno from inserted--the Noteno here to match the following
Open YB
DECLARE @NoteNO varchar (50)--Noteno here to match the above, define a cursor variable @noteno to manipulate insered in the Noteno table.
FETCH NEXT from YB into @NoteNO--if the noteno here matches, move the cursor
while (@ @fetch_status =0)--0 operation succeeded, -1 FETCH statement failed or the row is not in the result set,-2 fetched 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--continue to move the cursor until the @ @fetch_status is not equal to.
End
Close YB--closing cursors
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.