SQL Server 游标的使用示例
Ø 简介
本文主要记录 MSSQL 中的游标使用示例,在有必要时方便借鉴查阅。游标一般定义在某段功能性的 SQL 语句中,或者存储过程中。之所以选择用它,是因为有时候无法使用简单的 SQL 语句满足我们需求,比如需要对结果集中的每一条数据,根据不同条件进行不同操作(CRUD),这时我们就可以使用游标来完成。
提示:来之 DBA 的杰作,哈哈~~
1. 示例1:
本示例,用于初始化某新表的数据。使用游标遍历查询结果集,根据遍历的数据再插入另外两张表。SQL 代码如下:
IF(OBJECT_ID('SP_Init_CustomerNewOpening', :
1) 本示例,创建了一个存储过程,创建了两个游标。
2) 第一个游标,使用游标完成更新遍历的记录,再使用遍历的数据完成对另外两张表的 Insert 操作。
3) 第二个游标,完成对3张表的更新操作,在示例中使用了当前游标记录作为更新条件,例如:WHERE CURRENT OF cursor_Crm_Refundment。
4) 该存储过程用于定时任务执行,比在程序中去查询数据,然后再遍历写入数据效率上还是比较有优势的,而且便于维护。SQL 代码如下:
IF(OBJECT_ID('SP_Add_CustomerNewOpening', 'P') IS NOT NULL)
DROP PROCEDURE SP_Add_CustomerNewOpening;
GO
CREATE PROCEDURE SP_Add_CustomerNewOpening
(
@StartTime datetime, --开始时间
@EndTime datetime --结束时间
)
AS
DECLARE @Remark varchar(8000) = NULL;
INSERT INTO Sys_DbLog(LogTime, Type, Item, Message, Remark) VALUES(GETDATE(), 'info', 'SP_Add_CustomerNewOpening', '存储过程执行开始', NULL);
DECLARE @RewardsMoney decimal(18,2) = CAST(ISNULL((SELECT Value FROM Config WHERE Id=79), '0') AS decimal(18,2));
DECLARE @NewOpenMoney decimal(18,2) = CAST(ISNULL((SELECT Value FROM Config WHERE Id=80), '0') AS decimal(18,2));
DECLARE @AddCustNewOpenCount int = 0, @AddOrderNewOpenCount int = 0, @UpdateCustNewOpenCount int = 0,
@UpdateOrderNewOpenCount int = 0, @UpdateRefundmentCount int = 0;
BEGIN TRY
BEGIN TRANSACTION;
--1. 新增新开客户
DECLARE @OrderNewOpenId int, @CustNewOpenId int;
DECLARE @UserId bigint, @PayTime datetime, @Address nvarchar(500), @CustomerId bigint, @EmplCityId int, @EmployeeId int, @RealTotal decimal(18,2);
DECLARE cursor_CustomerNewOpening CURSOR GLOBAL SCROLL DYNAMIC SCROLL_LOCKS
FOR SELECT UserId, PayTime, Address, CustomerId, EmplCityId, EmployeeId, SUM(RealTotal) AS RealTotal FROM
(
SELECT t1.UserId, t1.PayTime, t1.Address, t2.CustomerId, t4.CityId AS EmplCityId, t4.EmployeeId, t1.RealTotal FROM Orders AS t1
INNER JOIN UserInfo AS t2 ON(t1.UserId = t2.Id)
INNER JOIN Customer AS t3 ON(t2.CustomerId = t3.Id)
INNER JOIN Sys_EmployeeProfile AS t4 ON(t1.SalesUserId = t4.EmployeeId)
WHERE 1=1
AND t1.UserId > 0
AND t2.CustomerId > 0
AND t1.SalesUserId > 0
AND (t1.OrderStatusId > 1 AND t1.OrderStatusId < 10)
AND (t1.PayStatusId = 2 AND t1.PayTime >= @StartTime AND t1.PayTime <= @EndTime)
AND NOT EXISTS(SELECT TOP 1 1 FROM Crm_CustomerNewOpening AS T WHERE T.CustomerId = t2.CustomerId AND (T.NewOpenStatus = 1 OR T.NewOpenStatus = 4))
AND NOT EXISTS(SELECT TOP 1 1 FROM Crm_OrderNewOpening AS T WHERE T.OrderId = t1.Id)
) AS T WHERE 1=1
AND RealTotal >= @NewOpenMoney
GROUP BY UserId, PayTime, Address, CustomerId, EmplCityId, EmployeeId;
OPEN cursor_CustomerNewOpening;
FETCH FIRST FROM cursor_CustomerNewOpening INTO @UserId, @PayTime, @Address, @CustomerId, @EmplCityId, @EmployeeId, @RealTotal;
WHILE(@@FETCH_STATUS = 0)
BEGIN
--防止本次执行多条符合的情况
IF(NOT EXISTS(SELECT TOP 1 1 FROM Crm_CustomerNewOpening AS T WHERE T.CustomerId = @CustomerId AND (T.NewOpenStatus = 1 OR T.NewOpenStatus = 4)))
BEGIN
--插入客户新开
INSERT INTO Crm_CustomerNewOpening(CustomerId, EmplCityId, EmployeeId, RewardsMoney, NewOpenMoney, RealTotal,
RefundmentMoney, PayTime, NewOpenStatus, AuditorId, AuditorTime, AuditDesc, CreateTime, UpdateTime)
VALUES(@CustomerId, @EmplCityId, @EmployeeId, @RewardsMoney, @NewOpenMoney, @RealTotal,
0, @PayTime, 1/*当前有效*/, NULL, NULL, NULL, GETDATE(), NULL);
SELECT @CustNewOpenId = SCOPE_IDENTITY(), @AddCustNewOpenCount = (@AddCustNewOpenCount + @@ROWCOUNT);
--插入订单新开
INSERT INTO Crm_OrderNewOpening(CustNewOpenId, OrderId, OrderNum, RealTotal, RefundmentMoney, CreateTime, UpdateTime)
SELECT @CustNewOpenId, t1.Id, t1.OrderNum, t1.RealTotal, 0, GETDATE(), NULL FROM Orders AS t1
WHERE 1=1
AND t1.UserId = @UserId
AND t1.PayTime = @PayTime
AND t1.Address = @Address;
SELECT @AddOrderNewOpenCount = (@AddOrderNewOpenCount + @@ROWCOUNT);
END
FETCH NEXT FROM cursor_CustomerNewOpening INTO @UserId, @PayTime, @Address, @CustomerId, @EmplCityId, @EmployeeId, @RealTotal;
END
CLOSE cursor_CustomerNewOpening;
DEALLOCATE cursor_CustomerNewOpening;
--2. 更新客户新开(退款失效)
DECLARE @RefundmentId int, @OrderId bigint, @RefundmentMoney decimal(18,2);
DECLARE cursor_Crm_Refundment CURSOR GLOBAL SCROLL DYNAMIC SCROLL_LOCKS
FOR SELECT Id, OrderId, RefundmentMoney FROM Crm_Refundment AS t1 WHERE t1.OrderNewOpenId IS NULL
AND t1.CreateTime >= @StartTime AND t1.CreateTime <= @EndTime
FOR UPDATE OF OrderNewOpenId, UpdateTime;
OPEN cursor_Crm_Refundment;
FETCH FIRST FROM cursor_Crm_Refundment INTO @RefundmentId, @OrderId, @RefundmentMoney;
WHILE(@@FETCH_STATUS = 0)
BEGIN
SELECT @OrderNewOpenId = NULL, @CustNewOpenId = NULL;
SELECT @OrderNewOpenId = Id, @CustNewOpenId = CustNewOpenId FROM Crm_OrderNewOpening WHERE OrderId = @OrderId;
IF(@OrderNewOpenId IS NOT NULL AND @CustNewOpenId IS NOT NULL)
BEGIN
DECLARE @RowCount1 int = 0, @RowCount2 int = 0;
--1. 更新订单新开
UPDATE Crm_OrderNewOpening SET RefundmentMoney = (RefundmentMoney + @RefundmentMoney), UpdateTime = GETDATE()
WHERE Id = @OrderNewOpenId;
SELECT @RowCount1 = @@ROWCOUNT, @UpdateOrderNewOpenCount = (@UpdateOrderNewOpenCount + @@ROWCOUNT);
--2. 更新客户新开
UPDATE Crm_CustomerNewOpening SET RefundmentMoney = (RefundmentMoney + @RefundmentMoney),
NewOpenStatus = (CASE WHEN NewOpenStatus = 1 AND (RealTotal - (RefundmentMoney + @RefundmentMoney)) < @NewOpenMoney
THEN 2 ELSE NewOpenStatus END)/*只有【当前有效】状态才更新状态*/,
RefundmentId = (CASE WHEN NewOpenStatus = 1 AND (RealTotal - (RefundmentMoney + @RefundmentMoney)) < @NewOpenMoney
--AND RefundmentId IS NULL/*逻辑上可以省略该条件,因为。。。*/
THEN @RefundmentId ELSE RefundmentId END)/*记录导致退款失效的退款Id*/,
UpdateTime = GETDATE()
WHERE Id = @CustNewOpenId;
SELECT @RowCount2 = @@ROWCOUNT, @UpdateCustNewOpenCount = (@UpdateCustNewOpenCount + @@ROWCOUNT);
IF(@RowCount1 = 1 AND @RowCount2 = 1)
BEGIN
UPDATE Crm_Refundment SET OrderNewOpenId = @OrderNewOpenId, UpdateTime = GETDATE() WHERE CURRENT OF cursor_Crm_Refundment;
SELECT @UpdateRefundmentCount = (@UpdateRefundmentCount + @@ROWCOUNT);
END
END
FETCH NEXT FROM cursor_Crm_Refundment INTO @RefundmentId, @OrderId, @RefundmentMoney;
END
CLOSE cursor_Crm_Refundment;
DEALLOCATE cursor_Crm_Refundment;
COMMIT;
SET @Remark = '新开客户数:' + CAST(@AddCustNewOpenCount AS varchar(20))
+ ',新开订单数:' + CAST(@AddOrderNewOpenCount AS varchar(20))
+ ',更新新开客户数:' + CAST(@UpdateCustNewOpenCount AS varchar(20))
+ ',更新新开订单数:' + CAST(@UpdateOrderNewOpenCount AS varchar(20))
+ ',更新退款数:' + CAST(@UpdateRefundmentCount AS varchar(20));
INSERT INTO Sys_DbLog(LogTime, Type, Item, Message, Remark) VALUES(GETDATE(), 'info', 'SP_Add_CustomerNewOpening', '存储过程执行结束', @Remark);
END TRY
BEGIN CATCH
ROLLBACK;
DECLARE @Message varchar(8000) = '错误:[' + CAST(ERROR_LINE() AS nvarchar(20)) + '],' + ERROR_MESSAGE();
INSERT INTO Sys_DbLog(LogTime, Type, Item, Message, Remark) VALUES(GETDATE(), 'error', 'SP_Add_CustomerNewOpening', @Message, '事物已回滚');
END CATCH
GO
SQL Server 游标的使用示例的更多相关文章
- SQL Server游标 C# DataTable.Select() 筛选数据 什么是SQL游标? SQL Server数据类型转换方法 LinQ是什么? SQL Server 分页方法汇总
SQL Server游标 转载自:http://www.cnblogs.com/knowledgesea/p/3699851.html. 什么是游标 结果集,结果集就是select查询之后返回的所 ...
- SQL Server 游标运用:鼠标轨迹字符串分割
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 游标模板(Cursor Template) 鼠标轨迹字符串分割SQL脚本实现(SQL Code ...
- sql server 游标的简单用法
sql server游标: --定义游标 declare cursor1 cursor for select ID,Name from A --打开游标 open cursor1 declare @i ...
- SQL Server 游标运用:查看所有数据库所有表大小信息(Sizes of All Tables in All Database)
原文:SQL Server 游标运用:查看所有数据库所有表大小信息(Sizes of All Tables in All Database) 一.本文所涉及的内容(Contents) 本文所涉及的内容 ...
- SQL Server 游标运用:查看一个数据库所有表大小信息(Sizes of All Tables in a Database)
原文:SQL Server 游标运用:查看一个数据库所有表大小信息(Sizes of All Tables in a Database) 一.本文所涉及的内容(Contents) 本文所涉及的内容(C ...
- 学习使用MS SQL Server游标(CURSOR)
说实的,使用MS SQL Server这样久,游标一直没有使用过.以前实现相似的功能,都是使用WHILE循环加临时表来实现.刚才有参考网上示例练习写了一下.了解到游标概念与语法. 下面代码示例中,先是 ...
- Sql Server 游标概念与实例
引言 先不讲游标的什么概念,看如下Sql Server2008 图例: 需求:两张表的O_ID是一一对应的,现在求将加薪的工资+原来的工资=现在的工资,也就是O_Salary=O_Salary+A_S ...
- 2009-04-19 22:40 SQL SERVER游标的讲解
游标和游标的优点 在数据库中,游标是一个十分重要的概念.游标提供了一种对从表中检索出的数据进行操作的灵活手段,就本质而言,游标实际上是一种能从包括多条数据记录的结 果集中每次提取一条记录的机制.游标总 ...
- SQL Server游标(转)
清晰地介绍了SQL游标,很好的学习资料. 转自 http://www.cnblogs.com/knowledgesea/p/3699851.html 什么是游标 结果集,结果集就是select查询之后 ...
随机推荐
- Linux Mint有进程管理器吗?答案是肯定的
Linux Mint系统内置了一个系统管理器,叫 "System Monitor",通过 Menu -->> 系统工具 可以查看 . 如下图,在管理器上右键,可以&qu ...
- 你的知识需要管理PKM
有一段时间没有更新技术博客了~,大脑中总感觉有点东西要写,却不知道从哪里开始写~至少写点东西,也算是一个阶段的成长.反思~ 学习(充电过程).工作(知识变现过程)不是简单重复,永远都是最值得去反思.玩 ...
- 前端架构师 摘自《前端架构设计》-micah godbolt
作为前端架构师,你经常需要制定,或至少能够掌握以上所列的每一项内容.流程中的任何一个环节出现问题,都会迅速演变为开发人员的痛苦,或者导致网站无法持续满足用户要求,甚至崩溃. 前端架构师的用户是开发人 ...
- Facebook 广告投放相关概念简介(1)
本文不涉及具体代码实现,仅对开发API的前置内容做简单介绍,想参考代码请绕行! 广告主(广告管理工具) ·需要推广自己的应用.网站.主页,所以有了广告管理工具 . ·一个广告主仅可拥有一个广告账户(可 ...
- Day9 轨道角动量
转自中山大学电子与信息工程 http://seit.sysu.edu.cn/node/1004 能量.动量(角动量和线动量)光子的基本属性,其中光子角动量包括自旋角动量和轨道角动量(Orbital a ...
- Map the Debris 轨道周期
返回一个数组,其内容是把原数组中对应元素的平均海拔转换成其对应的轨道周期. 原数组中会包含格式化的对象内容,像这样 {name: 'name', avgAlt: avgAlt}. 至于轨道周期怎么求, ...
- iTextCharp c#
//引用iTextSharp static void testPdf() { var document = new Document(); var writer = PdfWriter.GetInst ...
- axios delete请求 参数
内容转载作者:https://www.cnblogs.com/pangguoming/p/8994742.html 如果服务端将参数当做 java对象来封装接收则 参数格式为:{data: param ...
- MVC中使用Hangfire按秒执行任务
更新Hangfire版本到1.7.0,才支持使用按秒循环任务执行 RecurringJob.AddOrUpdate("test",()=>writeLog("每20 ...
- c提高第四次作业
1. 简述指针数组和数组指针的区别?答: 指针数组:是一个数组,每个元素都是指针 数组指针:是一个指针,指向数组的指针 2. 如何定义一个指向 int a[10] 类型的指针变量(数组指针)(使用3种 ...