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

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) = ''
begin
exec proc_sqlser_insertqueue @oper_cart_id,@oper_cart_item_id, @the_last_changed_date, @oper_cust_id, '';
end update #shoppingcart_temp set flag = 1 where cart_item_id = @oper_cart_item_id
end

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

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) = ''
begin
exec proc_sqlser_insertqueue @oper_cart_id,@oper_cart_item_id, @the_last_changed_date, @oper_cust_id, '';
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

sqlserver 脚本 多条记录遍历的更多相关文章

  1. SqlServer插入1000条记录

    1.想在SqlServer中插入指定数量的测试记录怎么办? 2.解决: DECLARE @var INT ) BEGIN INSERT INTO test (Name) VALUES (convert ...

  2. 分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间)

    分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间) 很多时候我们都需要计算数据库中各个表的数据量和每行记录所占用空间 这里共享一个脚本 CREATE TABLE #tab ...

  3. (转)分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间)

    分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间) 很多时候我们都需要计算数据库中各个表的数据量和每行记录所占用空间 这里共享一个脚本 CREATE TABLE #tab ...

  4. 从SQLSERVER/MYSQL数据库中随机取一条或者N条记录

    从SQLSERVER/MYSQL数据库中随机取一条或者N条记录 很多人都知道使用rand()函数但是怎麽使用可能不是每个人都知道 建立测试表 USE [sss] GO ,NAME ) DEFAULT ...

  5. SQLServer 分组查询相邻两条记录的时间差

    原文:SQLServer 分组查询相邻两条记录的时间差 首先,我们通过数据库中表的两条记录来引出问题,如下图 以上为一个记录操作记录的表数据.OrderID为自增长列,后面依次为操作类型,操作时间,操 ...

  6. sqlserver -- 学习笔记(七)获取同组数据的前两条记录

    不啰嗦,直接上图,大概实现效果如下: 有上面这样一份数据,将他们按照userAccount和submitTime进行分组,然后提前每组数据的前两条记录 提取后数据如下: 实现的SQL如下: selec ...

  7. SQLServer 触发器 同时插入多条记录有关问题

    由于 SQL Server 的触发器, 没有 FOR EACH ROW (ORACL中有)的选项, 有时候不正确的使用 inserted 与deleted 可能会有点麻烦. 下面来一个简单的例子 -- ...

  8. sqlserver查询数据表中每个类别最新的一条记录

    表tariff_info, 原始数据: 想要的结果:以start_time时间倒序排序, 以code分类, 查询每一类最新的一条记录 sql: SELECT a.* FROM TARIFF_INFO ...

  9. 分享一个SQLSERVER脚本

    原文:分享一个SQLSERVER脚本 分享一个SQLSERVER脚本 很多时候我们都需要计算数据库中各个表的数据量很每行记录所占用空间 这里共享一个脚本 CREATE TABLE #tablespac ...

随机推荐

  1. linux平台学x86汇编语言学习集合帖

    linux平台学x86汇编语言学习集合帖 linux平台学x86汇编(一):https://blog.csdn.net/shallnet/article/details/45543237 linux平 ...

  2. python基础===获取知乎标题时候,文件编码失败的总结

    总结一下,关于获取到的信息编码失败. 刚才在执行代码的时候,发现一个问题: 然后修改代码如下: '''爬取知乎界面的标题''' import requests import re import sys ...

  3. 【HDU3037】Saving Beans

    Lucas的裸题,学习一个. #include<bits/stdc++.h> #define N 100010 using namespace std; typedef long long ...

  4. JS面试题第一弹

    1.javascript的typeof返回哪些数据类型  alert(typeof [1, 2]); //object     alert(typeof 'leipeng'); //string   ...

  5. 方便大家学习的Node.js教程(一):理解Node.js

    理解Node.js 为了理解Node.js是如何工作的,首先你需要理解一些使得Javascript适用于服务器端开发的关键特性.Javascript是一门简单而又灵活的语言,这种灵活性让它能够经受住时 ...

  6. LCA上的RMQ模板算法

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. umount.nfs: device is busy解决办法

    fuser -km /app/nfs/ https://blog.csdn.net/x_i_y_u_e/article/details/42914817

  8. 使用PuTTY连接树莓派

    这是 meelo 原创的 玩转树莓派 系列文章 PuTTY是一个支持Telnet.SSH协议,实现远程登录的软件.树莓派的官方操作系统Raspbian默认开启了SSH协议进行登录,这样即使没有专门的显 ...

  9. Apache配置基于端口号的虚拟主机 Apache virtual host configuration is based on the port

    有可能只有一个ip出口,但却有多个项目,那么就需要基于端口号架设虚拟主机. Step 1: 检查是否开启 httpd-vhosts.conf apache/conf/httpd.conf文件 # Vi ...

  10. Java字符串易错方法总结

    Java字符串易错方法总结 public String[] split(String regex) 和 public String[] split(String regex,int limit) li ...