Chapter 8 Transact-SQL Program Design
8.1_ variables
8.1.1_ local variables
Declaration definition of local variables:
Declare @Variable_name Datatype[, @Variable_name Datatype]…
--举例:
declare @s1 char(20),@s2 int
Where @Variable_name is the name of a local variable, it must start with @ and follow the SQL Server 2000 identifier and object naming convention
Assignment method:
initialize at the time of declaration:
declare @s2 int = 20
Settings:
set @s2 = 20
8.1.2_ Global Variables
SQL Server 2000 provides a system and assign variables, users can not create global variables , also can not use the SET statement to change the value of a global variable , it starts with @@ , most of the values of global variables is to report the occurrence of SQL Server 2000 system after start Activities, usually assign the value to a local variable for processing.
8.2_ Flow control statement
Let me talk about the statement block first, and begin is used here. . . The end statement is
begin...end is the curly braces in the C language.
Secondly, the print output uses print'string'
8.2.1_IF...ELSE statement
IF (SELECT ContractVolume FROM Contract WHERE ContractID=’101’) >10000
PRINT '该合同金额超过1万元'
ELSE
PRINT '该合同金额不足1万元'
8.2.2_while loop statement
--打印1到10
DECLARE @i INTEGER
DECLARE @iMAX INTEGER
SET @iMAX = 10
SET @i=1
WHILE @i<=@iMAX
BEGIN
PRINT @i
SET @i=@i+1
END