用友软件
思迅软件
其他软件
详细信息 您现在的位置:网站首页 >详细信息
 

sqlserver技巧if和while的使用

 
 
  1.   --                                  ╔════════╗    
  2.   -- ===============================  ║ if语句使用示例 ║    
  3.   --                                  ╚════════╝     
  4.             declare @a int    
  5.             set @a=12    
  6.             if @a>100    
  7.                begin    
  8.                    print @a    
  9.                end    
  10.             else    
  11.                begin    
  12.                    print 'no'    
  13.                end    
  14.   --                                  ╔══════════╗    
  15.   -- ===============================  ║ while语句使用示例  ║    
  16.   --                                  ╚══════════╝  
  17. declare @i int   
  18. set @i=1   
  19. while @i<30   
  20.    begin   
  21.    insert into test (userid) values(@i)   
  22.    set @i=@i+1   
  23. end  
  24.   
  25. -- 设置重复执行 SQL 语句或语句块的条件。只要指定的条件为真,就重复执行语句。可以使用 BREAK 和 CONTINUE 关键字在循环内部控制 WHILE 循环中语句的执行。 本条为以前从网上查找获取!  
  26.   
  27.   
  28.   --                                   ╔════════╗    
  29.   -- ================================  ║  临时表和try   ║    
  30.   --                                   ╚════════╝     
  31.      
  32.       -- 增加临时表    
  33.        select * into #csj_temp from csj    
  34.             
  35.        -- 删除临时表 用到try    
  36.         begin try    -- 检测代码开始    
  37.              drop table #csj_temp    
  38.         end try    
  39.      
  40.         begin catch  -- 错误开始    
  41.         end catch   
  42.   
  43.   
  44.  --                                  ╔═════════╗    
  45.  -- ===============================  ║ 游标循环读记录   ║    
  46.  --                                  ╚═════════╝     
  47.               
  48.             declare @temp_temp int    
  49.             --declare @Cur_Name    
  50.             --@Cur_Name="aaa"    
  51.             --------------------------------- 创建游标  --Local(本地游标)    
  52.             DECLARE aaa CURSOR for select House_Id from House_House where Deleted=0 or deleted is null    
  53.             ----------------------------------- 打开游标    
  54.               Open aaa    
  55.             ----------------------------------- 遍历和获取游标    
  56.                    
  57.             fetch next from aaa into @temp_temp    
  58.             --print @temp_temp    
  59.             while @@fetch_status=0    
  60.             begin    
  61.               --做你要做的事      
  62.               select * from House_monthEnd where House_Id=@temp_temp    
  63.      
  64.               fetch next from aaa into @temp_temp  -- 取值赋给变量    
  65.      
  66.              --     
  67.             end    
  68.      
  69.             ----------------------------------- 关闭游标    
  70.               Close aaa    
  71.             ----------------------------------- 删除游标    
  72.               Deallocate aaa    
  73.             --   
上一篇:全自助式点菜结账 安卓版无线点菜系统Ⅱ帮你省心省力 下一篇:Win10vpn连接不上的解决办法
版权所有:常熟市掌搜网络技术有限公司 Copyright © 2024 All rights reserved.
联系人:金波 手机:15150376005 电话: 15150376005
网址: www.hand-soft.net E-MAIL:24727898@qq.com
地址: 江苏省常熟市海虞镇奥特莱斯A11-229
苏ICP备14014920号-1