首页 > 数据库技术 > 详细

sqlserver 脚本 多条记录遍历

时间:2014-05-30 03:05:17      阅读:559      评论:0      收藏:0      [点我收藏+]

临时表方式实现多条记录遍历

bubuko.com,布布扣
declare @oper_cart_item_id bigint;
 declare @oper_cart_id bigint;
 declare @the_last_changed_date datetime;
 declare @oper_cust_id int;
 
 
     select * , 0 as flag into #shoppingcart_temp from deleted;
    while exists (select top 1 1 from #shoppingcart_temp t where t.flag = 0 )
    begin
  
    select top 1 
    @oper_cart_item_id = cart_item_id,@oper_cart_id = cart_id,
    @oper_cust_id = cust_id,@the_last_changed_date = last_changed_date
    from  #shoppingcart_temp where flag = 0 ;

    if @oper_cart_item_id is not null and @oper_cart_item_id <> 0 and left(@oper_cart_id,1) = 8
    begin
       exec proc_sqlser_insertqueue @oper_cart_id,@oper_cart_item_id, @the_last_changed_date, @oper_cust_id, 3;
    end
  
    update  #shoppingcart_temp  set  flag = 1 where cart_item_id = @oper_cart_item_id
  end
bubuko.com,布布扣

 

游标方式实现多条记录遍历

bubuko.com,布布扣
declare @oper_cart_item_id bigint;
 declare @oper_cart_id bigint;
 declare @the_last_changed_date datetime;
 declare @oper_cust_id int;
 
    declare shoppingcart_cursor cursor for select cart_item_id,cart_id,cust_id,last_changed_date from deleted;
 
    open shoppingcart_cursor
   
    fetch next from shoppingcart_cursor into @oper_cart_item_id,@oper_cart_id, @oper_cust_id,@the_last_changed_date
   
    while (@@fetch_status=0)
    begin
 
        if @oper_cart_item_id is not null and @oper_cart_item_id <> 0 and left(@oper_cart_id,1) = 8
        begin
           exec proc_sqlser_insertqueue @oper_cart_id,@oper_cart_item_id, @the_last_changed_date, @oper_cust_id, 3;
        end
        fetch next from shoppingcart_cursor into @oper_cart_item_id,@oper_cart_id, @oper_cust_id,@the_last_changed_date
    end
    close shoppingcart_cursor
    DEALLOCATE shoppingcart_cursor
bubuko.com,布布扣

 

 

sqlserver 脚本 多条记录遍历,布布扣,bubuko.com

sqlserver 脚本 多条记录遍历

原文:http://www.cnblogs.com/olmlo/p/3757802.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!