ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

SqlServer中循环和条件语句

2021-04-13 15:01:51  阅读:150  来源: 互联网

标签:语句 aaa temp -- SqlServer 游标 循环 end



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

标签:语句,aaa,temp,--,SqlServer,游标,循环,end
来源: https://blog.51cto.com/lhrbest/2703353

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有