sql service 游标和触发器的使用
一、 定义游标
使用游标相当于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 游标和触发器的使用的更多相关文章
- Sql Service 存储过程、触发器
if exists (select * from sysobjects where name='tb_admin') drop table tb_admin go create table tb_ad ...
- SQL Server 存储过程、触发器、游标
存储过程 1.存储过程是事先编好的.存储在数据库中的程序,这些程序用来完成对数据库的指定操作. 2.系统存储过程: SQL Server本身提供了一些存储过程,用于管理有关数据库和用户的信息. 用户存 ...
- SQL Server -- 回忆笔记(五):T-SQL编程,系统变量,事务,游标,触发器
SQL Server -- 回忆笔记(五):T-SQL编程,系统变量,事务,游标,触发器 1. T-SQL编程 (1)声明变量 declare @age int (2)为变量赋值 (3)while循环 ...
- Oracle学习2 视图 索引 sql编程 游标 存储过程 存储函数 触发器
---视图 ---视图的概念:视图就是提供一个查询的窗口,来操作数据库中的数据,不存储数据,数据在表中. ---一个由查询语句定义的虚拟表. ---查询语句创建表 create table emp a ...
- MYSQL存储过程、游标、触发器
MySQL5 中添加了存储过程的支持. 大多数SQL语句都是针对一个或多个表的单条语句.并非所有的操作都怎么简单.经常会有一个完整的操作需要多条才能完成 存储过程简单来说,就是为以后的使用而保存的一 ...
- 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. ...
- 四、Oracle loop循环、while循环、for循环、if选择和case选择、更改读取数据、游标、触发器、存储过程
数据库的设计(DataBase Design): 针对于用户特定的需求,然后我们创建出来一个最使用而且性能高的数据库! 数据库设计的步骤: 01.需求分析 02.概念结构设计 03.逻辑结构设计 04 ...
- 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 ...
随机推荐
- ModSecurity防御暴力破解
http://www.modsecurity.org/ ModSecurity防御暴力破解 在阅读本文前,先简单了解下什么是ModSecurity,ModSecurity是一个入侵探测与阻止的引擎.它 ...
- Multivariate Linear Regression
Multiple Features Linear regression with multiple variables is also known as "multivariate line ...
- 2.3系列系统中不支持SimpleDateFormat作字段被序列化
安卓问题记录:在2.3系列系统中不支持SimpleDateFormat作字段被序列化,使用时需要将SimpleDateFormat作临时变量使用.
- 正确理解Spring事务和数据库事务和锁
Lock wait timeout exceeded; try restarting transaction解决方案 参考文章 Spring中@Transactional事务回滚 http://www ...
- [Http] Understand what an HTTP Request is
Let's look at several HTTP requests to learn the basic structure of these messages, and how the vari ...
- [ES7] Convert Any Function into an Asynchronous Function
Any function can be made asynchronous, including function expressions, arrow functions, and methods. ...
- 在线算法与离线算法(online or offline)
1. 在线算法(online) PFC(prefix-free code)编码树的解码过程:可以在二进制编码串的接收过程中实时进行,而不必等到所有比特位都到达后才开始: 2. 离线算法(offline ...
- eclipse 远程debug tomcat web项目
1.首先须要在linux系统tomcat/bin文件夹下配置catalina.sh这个文件里添加: CATALINA_OPTS="-Xdebug -Xrunjdwp:transport=d ...
- html5-8 如何控制html5中的视频标签和音频标签
html5-8 如何控制html5中的视频标签和音频标签 一.总结 一句话总结:找到视频或者音频的element对象,然后查手册看对应的方法或者属性就可以,里面有控制的. 1.如何控制html5中的视 ...
- [Win 8/WP 8]简单实现弹出页更换头像的功能
在Win 8应用里,对弹出页(Popup)的灵活操作必不可少,下面我们就来简单实现一个. 一.先让Popup弹出到指定位置 先来看看效果图,如图[1]: 下面是前端代码,代码段[1]: <Gri ...