-- Variable declaration. The @ symbol must be added before the variable when the SQL statement declares the variable.
Declare @ I int
-- Assign a value to a variable. set must be added before the variable is assigned a value.
Set @ I = 30
-- Declare multiple variables
Declare @ s varchar (10), @ A int
-- If statement in SQL
If condition begin
Execution statement
End
Else begin
Execution statement
End
Declare @ d int
Set @ d = 1
If @ d = 1 begin
-- Print
Print 'true'
End
Else begin
Print 'error'
End
-- Multi-condition selection statement in SQL.
Declare @ iret int, @ pkdisp varchar (20)
Set @ iret = 1
Select @ iret =
Case
When @ pkdisp = '1' then 1
When @ pkdisp = '2' then 2
When @ pkdisp = '3' then 3
When @ pkdisp = '4' then 4
When @ pkdisp = '5' then 5
The else 100
End
-- Loop statement
While condition begin
Execution statement
End
Declare @ I int
Set @ I = 1
While @ I <1000000 begin
Set @ I = @ I + 1
End
-- Print
Print @ I
-- Select into creates a new table from the computing result of a query. The data is not returned to the client.
-- Select is different. The fields in the new table have the names and data types associated with the select output fields.
Select * into newtable
From uname
-- Insert into select
-- Table ABC must exist
-- Copy the username field in the table uname to table ABC
Insert into ABC select username from uname
-- Create a temporary table
Create Table # temp (
UID int identity (1, 1) primary key,
Username varchar (16 ),
PWD varchar (50 ),
Age smallint,
Sex varchar (6)
)
-- Open a temporary table
Select * from # temp
-- Stored Procedure
-- Database for which the stored procedure is to be created
Use test
-- Determine whether the stored procedure name to be created exists
If exists (Select name from sysobjects where name = 'csp _ addinfo' and
Type = 'P ')
-- Delete a stored procedure
Drop procedure DBO. csp_addinfo
Go
-- Create a stored procedure
Create proc DBO. csp_addinfo
-- Stored procedure parameters
@ Username varchar (16 ),
@ PWD varchar (50 ),
@ Age smallint,
@ Sex varchar (6)
As
-- Stored procedure statement body
Insert into uname (username, PWD, Age, Sex)
Values (@ username, @ PWD, @ age, @ sex)
Return
-- Execute
Go
-- Execute the Stored Procedure
Exec csp_addinfo 'junn. A', '201312', 20, 'male'
Questions about game standards
Declare dB cursor for // specify a swimming standard (dB for its name)
Select unit_name from unit where left (unit_code, 2) = left (@ strto, 2) // records the dataset
Open dB // open the game
Fetch next from DB into @ strunit_name // place the first value in a batch
While @ fetch_status = 0 --- the existence of the condition value follows the condition
(0: profitable rows;-1: loss, or the data column exceeds the result set;-2: The retrieved data column has been leaked)
Begin ---- start cycle
// Personal operation
End ---- end point
Close dB --- standards
Deallocate dB // the benchmark for removing information