SQL中循环和条件语句

1、if语句使用示例:
     declare @a int
     set @a=12
     if @a>100
        begin
            print @a
        end    
    else
        begin
            print 'no'
        end

2、while语句使用示例:
      declare @i int
      set @i=1
      while @i<30
            begin
            insert into test (userid) values(@i)
            set @i=@i+1
        end
 设置重复执行SQL语句或语句块的条件。只要指定的条件为真,就重复执行语句。可以使用BREAK   和CONTINUE关键字在循环内部控制WHILE循环;

3、临时表和try
     --增加临时表
    select * into #csj_temp from csj
    --删除临时表 用到try
    begin try   --检测代码开始
            drop table #csj_temp
    end try

    begin catch --错误开始
    end catch

4、游标循环记录
    declare @temp_temp int
    --创建游标 --Local(本地游标)
    DECLARE aaa CURSOR for select House_Id from             House_House where Deleted=0 or deleted is null
    --打开游标
    Open aaa
    --遍历和获取游标
    fetch next from aaa into @temp_temp
    --print @@fetch_status=0
    begin
    select * from House_monthEnd where House_Id=@temp_temp
    fetch next from aaa into @temp_temp --取值赋给变量
    end
    --关闭游标
    Close aaa
    --删除游标
    Deallocate aaa

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值