SQL控制程序执行流程

1. BEGIN...END语句

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排序


5. WHILE循环

6. WHILE…CONTINUE…BREAK

 declare @n int,@sum int
  select @n=1,@sum=0
  while @n<=20
  begin
  set @n=@n+1
  if @n%2=0
     set @sum=@sum+@n
  else
     continue
     print @n
     print'只有@n是偶数才输出这句话' 
  end 
  print @sum

7. RETURN退出


8. GOTO跳转

DECLARE @X INT   
set @x=1                                  
loving:
print @x
 set @x=@x+1
while @X<=3 goto loving

9. WAITFOR挂起

WAITFOR TIME'16:03:00'

PRINT'蒲亚静爱夏晶'


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值