SQL note (3) cursor

Source: Internet
Author: User

Create Database book

Create Table Booker (
Bookno int primary key not null,
Bookname varchar (50) not null,
Bookprice float not null,
Bookcount int not null,
Bookwriter varchar (50) not null
)

Insert into Booker values (1, 'wang Xiaojing ', 'Li Chunchun ')
Insert into Booker values (2, 'wang Xiaojing 2', 'Li Chunchun 2 ')
Insert into Booker values (3, 'wang Xiaojing 3', 'Li Chunchun 3 ')

Select * From Booker
------- Global variable ------------------------
Select @ version -- view the SQL version
Select @ error -- returns the last error of the executed Transact-SQL statement.Code.
------- Variable declaration --------------------------------------------------
Declare @ variable name int -- Declaration
Set @ variable name = 10 -- Value assignment
Select @ variable name -- the output variable value is valid at the same time in the preceding three steps

If (1 = 1)
Begin
Select * From Booker
End

---------------------------------------------- Cursor -------

--------------------------. 1 declaration cursor Syntax: declare cursor name cursor for select_statement
Declare cursor_name cursor
For select bookno, bookname from Booker where bookcount = 25
--------------------------. 2 open cursor Syntax: Open cursor name
Open cursor_name
--------------------------. 3 syntax for extracting cursor values: Fetch next from cursor name while (@ fetch_status = 0) Begin fetch next from cursor name end
Fetch next from cursor_name while (@ fetch_status = 0) -- returns the status of the last cursor executed by the fetch statement, rather than the status of any currently opened cursor.
-- Return value description
-- 0 fetch statement is successful.
---1 The fetch statement fails or this row is not in the result set.
---2 The extracted row does not exist.

Begin
Fetch next from cursor_name
End
--------------------------. 4 close the cursor close name
Close cursor_name
--------------------------. 5 delete the cursor deallocate.
Deallocate cursor_name

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.