Use instances with cursors-entry-level

Source: Internet
Author: User

 
Example
The Table1 structure is as follows:
Id int
Name varchar (50)

Declare @ ID int
Declare @ name varchar (50)
Declare cursor1 cursor for -- defines the cursor cursor1
Select * From Table1 -- the object using the cursor (enter select text as needed)
Open cursor1 -- open a cursor

Fetch next from cursor1 into @ ID, @ name -- move the cursor down one row and put the obtained data in the variable @ ID, @ name

While @ fetch_status = 0 -- determines whether data is successfully obtained
Begin
Update Table1 set name = Name + '1'
Where [email protected] -- perform corresponding processing (enter the SQL text as needed)

Fetch next from cursor1 into @ ID, @ name -- move the cursor down one row
End

Close cursor1 -- close the cursor
Deallocate cursor1

Alter trigger [DBO]. [custom_mytextbooks_update_isopen_vocabulary]

On [DBO]. [custom_mytextbooks] for update
As

If Update ([isopen])
Begin

Declare cursorupdate cursor
Select inserted. bookid, inserted. childid, inserted. isopen isopeninserted, deleted. isopen isopendeleted from inserted inner join deleted
On inserted. myid = deleted. myid

Open cursorupdate

Declare @ bookid int, @ childid int, @ isopeninserted bit, @ isopendeleted bit

Fetch next from cursorupdate into @ bookid, @ childid, @ isopeninserted, @ isopendeleted

While @ fetch_status = 0
Begin

If @ isopendeleted = 0 and @ isopeninserted = 1
Begin

Declare @ RET int;
Exec @ ret = trigger_create_vocabulary_by_mytextbooks
@ Childid = @ childid,
@ Bookid = @ bookid
End

Fetch next from cursorupdate into @ bookid, @ childid, @ isopeninserted, @ isopendeleted
End


Close cursorupdate
Deallocate cursorupdate


End

 

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.