SQL cursor sorting

Source: Internet
Author: User

Recently I have seen a headache with cursor problems. Refresh the cursor.

1 declare @ id varchar (50 );
2 declare @ Title varchar (50 );
3 declare @ Director varchar (50 );
4 declare @ DateReleased varchar (50 );
5
6 declare mycursor cursor for -- defines a cursor
7 select * from movies -- The cyclable object of the cursor
8 open mycursor -- open the cursor
9 fetch next from mycursor into @ id, @ Title, @ Director, @ DateReleased -- move the cursor to the next data (the first one), get the data @ id, @ Title, @ Director, @ DateReleased (the number must be the same)
10 while (@ FETCH_STATUS = 0) -- determines whether data is obtained successfully.
11 /*
12 @ FETCH_STATUS = 0 The FETCH statement is successful.
13 @ FETCH_STATUS =-1 The FETCH statement fails or this row is not in the result set
14 @ FETCH_STATUS =-2 The extracted row does not exist.
15 */
16
17 begin
18
19 print (@ Title) -- operation on the cursor
20 fetch next from mycursor into @ id, @ Title, @ Director, @ DateReleased -- move the cursor down one line
21 end
22 close mycursor -- close the cursor
23 deallocate mycursor -- delete a cursor

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.