SqlServer中循环和条件语句示例!

  --                                  ╔════════╗  
  -- ===============================  ║ if语句使用示例 ║  
  --                                  ╚════════╝   
            declare @a int  
            set @a=12  
            if @a>100  
               begin  
                   print @a  
               end  
            else  
               begin  
                   print 'no'  
               end  
  --                                  ╔══════════╗  
  -- ===============================  ║ 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 循环中语句的执行。 本条为以前从网上查找获取!


  --                                   ╔════════╗  
  -- ================================  ║  临时表和try   ║  
  --                                   ╚════════╝   
   
      -- 增加临时表  
       select * into #csj_temp from csj  
          
       -- 删除临时表 用到try  
        begin try    -- 检测代码开始  
             drop table #csj_temp  
        end try  
   
        begin catch  -- 错误开始  
        end catch 


 --                                  ╔═════════╗  
 -- ===============================  ║ 游标循环读记录   ║  
 --                                  ╚═════════╝   
            
            declare @temp_temp int  
            --declare @Cur_Name  
            --@Cur_Name="aaa"  
            --------------------------------- 创建游标  --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 @temp_temp  
            while @@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  
            --      











 



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

敦厚的曹操

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值