一、 定义游标

使用游标相当于C#里面的集合。

declare @id nvarchar(20)
DECLARE My_Cursor CURSOR --定义游标
FOR (select autoid from U_VoucherItems where CardNum='k006' and CardSection='B') --查出需要的集合放到游标中
OPEN My_Cursor; --打开游标
FETCH NEXT FROM My_Cursor INTO @id; --读取第一行数据
WHILE @@FETCH_STATUS = 0
BEGIN
update U_VoucherItems set CardItemNum=(select MAX(CardItemNum)+1 from U_VoucherItems where CardNum='k006' and CardSection='B'
) where CardNum='k006' and autoid=@id FETCH NEXT FROM My_Cursor INTO @id; --读取第一行数据 END
CLOSE My_Cursor; --关闭游标
DEALLOCATE My_Cursor; --释放游标

二、触发器和游标一起使用

例子

/****** Object:  Trigger [dbo].[tgr_changeprice_delete]    Script Date: 03/25/2016 11:33:41 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create trigger [dbo].[tgr_changeprice_delete]
on [dbo].[SA_SaleDelivery_b]
instead of delete
as
declare @s_jsQuantity nvarchar(100),@zQuantity nvarchar(100),@s_wjsQuantity nvarchar(100),
@ClearingMoney nvarchar(100),@yxQuantity nvarchar(100),@NotClearingMoney nvarchar(100),
@Price nvarchar(100),@changepricedetailerid nvarchar(100); --定义变量
DECLARE My_Cursor CURSOR --定义游标
FOR (select quantity2,quantity,OrigDiscountPrice,sourceVoucherDetailId from deleted) --查出需要的集合放到游标中
OPEN My_Cursor; --打开游标
FETCH NEXT FROM My_Cursor INTO @zQuantity,@s_jsQuantity,@Price,@changepricedetailerid; --读取第一行数据
if(@changepricedetailerid is not null) --判断
begin
print 'start......'
WHILE @@FETCH_STATUS = 0
BEGIN
set @s_wjsQuantity=CONVERT (decimal(19,2),@s_jsQuantity);
set @ClearingMoney=CONVERT (decimal(19,2),@Price)*CONVERT (decimal(19,2),@s_jsQuantity);
set @NotClearingMoney=CONVERT (decimal(19,2),@Price)*CONVERT (decimal(19,2),@s_wjsQuantity); update nsc_changeprice_b set
yxQuantity= yxQuantity+CONVERT (decimal(19,2),@s_jsQuantity),
jsQuantity=jsQuantity-CONVERT (decimal(19,2),@s_jsQuantity),
wjsQuantity=wjsQuantity+CONVERT (decimal(19,2),@s_wjsQuantity),
ClearingMoney=ClearingMoney-CONVERT (decimal(19,2),@ClearingMoney),
NotClearingMoney=NotClearingMoney+CONVERT (decimal(19,2),@NotClearingMoney)
where id=@changepricedetailerid
FETCH NEXT FROM My_Cursor INTO @zQuantity,@s_jsQuantity,@Price,@changepricedetailerid; --读取第一行数据
END
CLOSE My_Cursor; --关闭游标
DEALLOCATE My_Cursor; --释放游标
end else
begin
print 'end.....'
--rollback transaction --回滚﹐避免加入
end

sql service 游标和触发器的使用的更多相关文章

  1. Sql Service 存储过程、触发器

    if exists (select * from sysobjects where name='tb_admin') drop table tb_admin go create table tb_ad ...

  2. SQL Server 存储过程、触发器、游标

    存储过程 1.存储过程是事先编好的.存储在数据库中的程序,这些程序用来完成对数据库的指定操作. 2.系统存储过程: SQL Server本身提供了一些存储过程,用于管理有关数据库和用户的信息. 用户存 ...

  3. SQL Server -- 回忆笔记(五):T-SQL编程,系统变量,事务,游标,触发器

    SQL Server -- 回忆笔记(五):T-SQL编程,系统变量,事务,游标,触发器 1. T-SQL编程 (1)声明变量 declare @age int (2)为变量赋值 (3)while循环 ...

  4. Oracle学习2 视图 索引 sql编程 游标 存储过程 存储函数 触发器

    ---视图 ---视图的概念:视图就是提供一个查询的窗口,来操作数据库中的数据,不存储数据,数据在表中. ---一个由查询语句定义的虚拟表. ---查询语句创建表 create table emp a ...

  5. MYSQL存储过程、游标、触发器

    MySQL5 中添加了存储过程的支持. 大多数SQL语句都是针对一个或多个表的单条语句.并非所有的操作都怎么简单.经常会有一个完整的操作需要多条才能完成  存储过程简单来说,就是为以后的使用而保存的一 ...

  6. ORACLE PL/SQL编程:把触发器说透

    本篇主要内容如下: 8.1 触发器类型 8.1.1 DML触发器 8.1.2 替代触发器 8.1.3 系统触发器 8.2 创建触发器 8.2.1 触发器触发次序 8.2.2 创建DML触发器 8.2. ...

  7. 四、Oracle loop循环、while循环、for循环、if选择和case选择、更改读取数据、游标、触发器、存储过程

    数据库的设计(DataBase Design): 针对于用户特定的需求,然后我们创建出来一个最使用而且性能高的数据库! 数据库设计的步骤: 01.需求分析 02.概念结构设计 03.逻辑结构设计 04 ...

  8. SQL Server游标 C# DataTable.Select() 筛选数据 什么是SQL游标? SQL Server数据类型转换方法 LinQ是什么? SQL Server 分页方法汇总

    SQL Server游标   转载自:http://www.cnblogs.com/knowledgesea/p/3699851.html. 什么是游标 结果集,结果集就是select查询之后返回的所 ...

  9. SQL Server 游标运用:鼠标轨迹字符串分割

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 游标模板(Cursor Template) 鼠标轨迹字符串分割SQL脚本实现(SQL Code ...

随机推荐

  1. [Node] Convert CommonJS Requires to ES6 Imports

    In this lesson we'll use cjs-to-es6 to convert CommonJS requires to ES6 imports. We'll also show how ...

  2. php 时间戳转为多少分钟前 小时前 天前

    function mdate($time = NULL) { $text = ''; $time = $time === NULL || $time > time() ? time() : in ...

  3. 细说document.ready和window.onload

    原文 简书原文:https://www.jianshu.com/p/bbf28d61aa1f 大纲 1.对页面加载的认识 2.关于document.ready() 3.关于document.onloa ...

  4. [CSS] Conditionally Apply Styles Using Feature Queries @supports

    While browsers do a great job of ignoring styles they don’t understand, it can be useful to provide ...

  5. js导出报表

    原文链接:https://blog.csdn.net/qq_37936542/article/details/78376156 需求:项目中有一个学生签到模块需要导出每天的签到数据,一开始用poi在后 ...

  6. 5.8 pprint--美观地打印数据

    pprint模块提供了一个美观地打印Python数据结构的方式.假设是要格式化的数据结构里包括了非基本类型的数据,有可能这样的数据类型不会被载入.比方数据类型是文件.网络socket.类等.本模块格式 ...

  7. 阿里巴巴fastjson的使用

    一.项目结构 一个学生类.当中学生类中能够包括Course类对象 使用Maven管理项目的能够加入fastjson的坐标: <dependency> <groupId>com. ...

  8. web项目中配置多个数据源

    web项目中配置多个数据源 spring + mybatis 多数据源配置有两种解决方案 1.配置多个不同的数据源,使用一个sessionFactory,在业务逻辑使用的时候自动切换到不同的数据源,  ...

  9. 你的薪水增速跑赢GDP了没

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZm9ydW9r/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...

  10. [Ramda] Filter an Array Based on Multiple Predicates with Ramda's allPass Function

    In this lesson, we'll filter a list of objects based on multiple conditions and we'll use Ramda's al ...