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