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

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. 基于Django Form源码开发自定义Form组件

    import copy import re class ValidateError(Exception): def __init__(self, detail): self.detail = deta ...

  2. 從 kernel source code 查出 版本號碼

    kernel/Makefile 1 VERSION = 4 2 PATCHLEVEL = 4 3 SUBLEVEL = 21 4 EXTRAVERSION = 5 NAME = Blurry Fish ...

  3. 64_m1

    MAKEDEV-3.24-18.fc26.x86_64.rpm 13-Feb-2017 22:33 101030 MUMPS-5.0.2-8.fc26.i686.rpm 14-Feb-2017 13: ...

  4. python爬虫模块之HTML下载模块

    HTML下载模块 该模块主要是根据提供的url进行下载对应url的网页内容.使用模块requets-HTML,加入重试逻辑以及设定最大重试次数,同时限制访问时间,防止长时间未响应造成程序假死现象. 根 ...

  5. bzoj 1015 星球大战starwar

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1015 题解: 如果按照题目的意思,每次删点.删边太困难了……于是采用逆向思维,构造出最后的 ...

  6. android 图片透明

    在ImageButton中载入图片后,图片周围会存在一圈白边,会影响到美观,其实解决这个问题有两种方法 一种方法是将ImageButton的背景改为所需要的图片.如:android:backgroun ...

  7. 数据库SQL实战(1)

    1.查找最晚入职员工的所有信息: CREATE TABLE `employees` ( `emp_no` int(11) NOT NULL, `birth_date` date NOT NULL, ` ...

  8. Leetcode 之Binary Tree Preorder Traversal(42)

    树的先序遍历.定义一个栈,先压入中间结点并访问,然后依次压入右.左结点并访问. vector<int> preorderTraversal(TreeNode *root) { vector ...

  9. 【mongo】用户添加、导入数据库、连接VUE

    添加用户 1.安装mongo时最好用apt-get install  因为这样可以省去很多麻烦,比如一些环境变量,还有一些文档路径等等的问题 2.确认一下自己的mongodb和mongodb-clie ...

  10. Dubbo简单DEMO以及重要配置项

    DEMO pom.xml 消费方和服务提供方一致 <properties> <spring.version>4.0.6.RELEASE</spring.version&g ...