SQL server cursor usage

Source: Internet
Author: User
1 -- zhxia
2 DECLARE @ username VARCHAR (20), @ flag BIT, @ ncount int
3 DECLARE mycursor cursor for -- DECLARE the CURSOR
4 SELECT user_name FROM RBAC_User
5 OPEN mycursor -- OPEN the cursor
6 SET @ flag = 0
7 SET @ ncount = 0
8 WHILE (@ flag = 0)
9 BEGIN
10 SET @ ncount = @ ncount + 1
11 FETCH mycursor INTO @ username -- cyclically read cursor
12 IF (@ FETCH_STATUS = 0)
13 UPDATE RBAC_User SET User_PassWord = @ ncount WHERE User_Name = @ username
14 ELSE
15 SET @ flag = 1
16 END
17 CLOSE mycursor -- CLOSE the cursor
18 DEALLOCATE mycursor -- release resources
19
20 @ FETCH_STATUS
21. Return the status of the last cursor executed by the FETCH statement, rather than the status of any currently opened cursor.
22 @ FETCH_STATUS = 0 success
23 @ FETCH_STATUS =-1 The FETCH statement fails or this row is not in the result set.
24 @ FETCH_STATUS =-2 The extracted row does not exist.
25 @ FETCH_STATUS = 100 is in the last row

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.