sql 循环语句几种方式

来源:互联网 发布:ip网络广播系统图 编辑:程序博客网 时间:2024/05/17 02:57
--第一

declare @orderNum varchar(255)
create table #ttableName(id int identity(1,1),Orders varchar(255))
declare @n int,@rows int
insert #ttableName(orders) select orderNum from pe_Orders where orderId<50
--select @rows=count(1) from pe_Orders
select @rows =@@rowcount 
set @n=1 
while @n<=@rows
begin
select @orderNum=OrderNum from PE_Orders where OrderNum=(select Orders from #ttableName where id=@n)
print (@OrderNum)
select @n=@n+1
end
drop table #ttableName


--第二

declare @orderN varchar(50)--临时变量,用来保存游标值
declare y_curr cursor for --申明游标 为orderNum
select orderNum from pe_Orders where orderId<50
open y_curr --打开游标
fetch next from Y_curr into @orderN ----开始循环游标变量
while(@@fetch_status=0)---返回被 FETCH  语句执行的最后游标的状态,而不是任何当前被连接打开的游标的状态。
begin
print (@orderN)
update pe_Orders set Functionary+@orderN where orderNum=@orderN --操作数据库
fetch next from y_curr into @orderN --开始循环游标变量
end
close y_curr--关闭游标
deallocate y_curr --释放游标

--第三
select orderNum,userName,MoneyTotal into #t from pe_Orders po 
DECLARE @n int,@error int
--set @n=1 
set @error=0
BEGIN TRAN --申明事务
declare @orderN varchar(50),@userN varchar(50) --临时变量,用来保存游标值
declare y_curr cursor for  --申明游标 为orderNum,userName
select orderNum,userName from PE_Orders where Orderid<50
open y_curr
fetch next from y_curr into @orderN,@userN
while @@fetch_status = 0
BEGIN
select isnull(sum(MoneyTotal),0),orderNum from #t where username=@userN
--set @n=@n+1
set @error=@error+@@error--记录每次运行sql后 是否正确  0正确
fetch next from y_curr into @orderN,@userN
END
IF @error=0
BEGIN
commit tran --提交
END
ELSE
BEGIN
ROLLBACK TRAN --回滚
END
close y_curr
deallocate y_curr
DROP TABLE #t

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