1. Variable Declaration and assignment
Global variables are predefined by the system and begin with the symbol @@ 打头
A local variable declaration uses the Declare statement, which must begin with a symbol @ followed by an identifier.
Declare @ variable name data type [, @ variable name data type, ...]
Variable assignment using the SELECT statement, the value of the unassigned variable is null.
Example:
Declare @msg Char (50)
Select @msg = ' How are '
Select @msg =emp_name from employee
Where emp_id=12345678
2. SQL statement block
Begin
Statement block/* SQL statements executed in multiple sequential order * *
End
3. Conditional statement
An If condition expression
Statement (block)
Else
Statement (block)
Example:
if (select Max (id) from sysobjects) <50
No user-created object in print ' Database
Else
Select Name,type,id from sysobjects where id>50
4. Loop statement
While condition expression
Statement (block)
Two special circular control statements:
Continue Execute Next loop
Break Exit Current Loop
Example:
while (select Avg. (price) from titles) >$20
Begin
Update titles Set PRICE=PRICE/2
If (select AVG (price) from titles) <$40
Break
Else
Continue
End
5. Other control statements
◇return Statement--an unconditional end to the current procedure and a State value that can be returned to the caller: return[integer expression]
◇print statement
◇raiseerror statement
◇waitfor statement