zoukankan      html  css  js  c++  java
  • sql 循环语句几种方式

    --第一
     
    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
     
    -------------------------------------------------------------
    转自http://www.cnblogs.com/Luouy/archive/2012/05/11/2495394.html
  • 相关阅读:
    使用IPTABLES限制IP上传下载速度,如何用iptables限速?
    基于queryperf 和 perftcpdns 的DNS压力测试
    搭建dnsmasq服务器,局域网内部解析
    Linux ipip隧道及实现
    使用FileZilla Server轻松搭建个人FTP服务器
    linux系统中如何查看日志 (常用命令)
    CentOS下安装XAMPP详细教程
    Tomcat中更改网站根目录
    php简单文件上传类
    SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
  • 原文地址:https://www.cnblogs.com/starm/p/4883364.html
Copyright © 2011-2022 走看看