一、 定义游标

使用游标相当于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. [Angular] Creating an Observable Store with Rx

    The API for the store is really simple: /* set(name: string, state: any); select<T>(name: stri ...

  2. iis windows phpstudy安装redis扩展

    说明,我的服务器是2008 64位 php5.4.33 首先下载符合条件的redis扩展,是否符合条件可以参考https://pecl.php.net/package/redis,进入之后,点击&qu ...

  3. tc

    [em_Compare]cmd=d:\Apps\BeyondCompare4\now\BCompare.exeparam="""%X%P%S"" &q ...

  4. jquery ajax实现省市二级联动

    今天给大家带来使用jQuery ajax实现的省市联动效果.我们直奔主题,先说下实现思路: 准备数据 这里数据库我使用的是mysql,先看下表格: provience表 city表 这里使用provi ...

  5. C# 使用 RabbitMQ

    1. RabbitMQ MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过写和检索出入列队的针对应用程序的数据(消息)来通信,而无需专用连接来链接 ...

  6. CISCO - 查找命令行

    http://www.cisco.com/c/en/us/support/web/tools/help/command_search_best_practices.html Support Best ...

  7. ArcGIS Engine 编辑- IWorkspaceEdit

    转自原文 ArcGIS Engine 编辑- IWorkspaceEdit 这个例子中,我创建了1000条要素,并结合缓冲将数据写到文件中,并且添加了时间统计,当然数据是我捏造的,还请原谅,这个花费的 ...

  8. IOS的后台执行

    写在前面给大家推荐一个不错的站点  www.joblai.com 本文章由央广传媒开发部 冯宝瑞整理.哈哈 http://www.cocoachina.com/bbs/read.php? tid=14 ...

  9. hadoop2.7全然分布式集群搭建以及任务測试

    要想深入的学习hadoop数据分析技术,首要的任务是必需要将hadoop集群环境搭建起来,本文主要讲述怎样搭建一套hadoop全然分布式集群环境. 环境配置:2台64位的redhat6.5 +  1台 ...

  10. DWG 对象与ArcGIS 要素的强制对应关系

    转自原文DWG 对象与ArcGIS 要素的强制对应关系 DWG 对象与ArcGIS 要素的强制对应关系如下: Feature type DWG object types   Point Point, ...