SQL控制程序执行流程
2. IF判断
其中命令行和程序块可以是合法Transact-SQL任意语句,但含两条或两条以上的语句的程序块必须加BEGIN...END子句
3. IF…ELSE选择
select'(' as 左括号,@x as '@x',@y as '@y' ,')' as 右括号,'在第三象限' as '所在象限'
select 输出时,后边加列名
4. CASE函数
case sex
when '1' then '男'
when '2' then '女’
else '其他' end
--case搜索函数
case when sex = '1' then '男'
when sex = '2' then '女'
else '其他' end
----------------------------------------------------------------------------------------------
USE db_2012
select kind = --kind字段下存放case的返回值
case ctype --比较ctype字段
when 'Computer' then 'COM' --ctype字段为Computer返回COM
when 'Bussiness' then 'BUS' --ctype字段为Bussiness返回BUS
end ,
cast(title as varchar(5)) as shortitle, --将title字段转换为5字节类型
price as PRICE --取别名PRICE
from instance413
where price is not null --约束为price不空
order by price --根据price排序
6. WHILE…CONTINUE…BREAK
declare @n int,@sum int
select @n=1,@sum=0
while @n<=20
begin
set @[email protected]+1
if @n%2=0
set @[email protected][email protected]
else
continue
print @n
print'只有@n是偶数才输出这句话'
end
print @sum
7. RETURN退出
8. GOTO跳转
DECLARE @X INT
set @x=1
loving:
print @x
set @[email protected]+1
while @X<=3 goto loving
9. WAITFOR挂起
WAITFOR TIME'16:03:00'
PRINT'蒲亚静爱夏晶'