sql service 游标和触发器的使用
2016-03-27 14:13
363 查看
一、 定义游标
使用游标相当于C#里面的集合。
二、触发器和游标一起使用
例子
使用游标相当于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
相关文章推荐
- 数据库_chapter05
- mysql5.6.28 备份主要参数学习
- sql server 向oracle导入表
- 向sql server 导入数据库
- mysql优化 之 表设计注意事项
- 数据库批量修改表名,增加前缀(SQL server)
- mysql中<>与null值不能比较
- JSP proxool+mysql数据库连接池配置
- windows下mysql忘记root密码的解决方法
- oracle常用函数
- [MySQL] 参数: innodb_flush_log_at_trx_commit和sync_binlog
- redis面试题总结
- SQL Server存储过程定义的几个最大值
- SQL(三)-- 练习题
- 使用c3p0连接mysql数据库
- mysql查询
- 2016/3/27 ①连接数据库方法的封装 ②连接数据库 制表 网页查询 奥迪
- Redis服务安装及PHP扩展
- Oracle BIEE BI Server ODBC 存储过程指南
- Redis-util 转换函数