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

posted on   快舔包我很肥  阅读(13531)  评论(0编辑  收藏  举报

编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示