sql service 游标和触发器的使用
2024-10-02 01:41:15
一、 定义游标
使用游标相当于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
最新文章
- tesseract 编译与使用(windows)
- sublime Text 2 制表符
- MVVM架构~knockoutjs系列之验证成功提示显示
- [Js]评分星星
- js之客户端检测
- 微信公众号与HTML 5混合模式揭秘2——分享手机相册中照片
- Jquery mobile 学习笔记
- asp.net 母版页使用详解--转
- [Practical Git] Remove unnecessary git tracking with .gitignore files
- Swift与Objective-C的兼容“黑魔法”:@objc和Dynamic
- DNS:因特网的目录服务
- .Net 异步随手记(一)
- PHPStorm+PHPStudy配置XDebug
- Java final类&;所有构造方法均为private的类(类型说明符&;访问控制符)
- 【读书笔记】iOS-button只显示在一个界面的右下角,不管界面大小怎么变化(xib,没有使用自动布局)(一)
- freemarker特殊字符转义
- ubuntu计划任务的编写
- Jmeter(三十六)纵横并发、限制QPS
- Android 设置EditText光标位置(转)
- android xml 解析汉字只出来一个字的问题
热门文章
- javascrit开发的基本代码结构的
- item.imageInsets =
- Vim常用匹配、查找、替换命令总结
- Windows Phone 8.1 后台任务
- POJ1659Frogs&;#39; Neighborhood(lavel定理)
- linux网络编程学习笔记之二 -----错误异常处理和各种碎碎(更新中)
- python启动应用程序和终止应用程序
- ssh和SSH服务(包含隧道内容)
- Python爬虫项目整理
- Java 中StringBuffer与StringBuilder区别(转)及String类的一些基本操作代码