【转】如何遍历一个结果集在 SQL Server 中使用 Transact-SQL
2011-11-01 16:41
537 查看
使用 Transact-SQL 语句来循环结果集
有三种方法可用于循环一个结果集通过使用 Transact-SQL 语句。一种方法是使用 临时 表。 使用此方法,您创建初始 SELECT 语句的"快照"并将其用作基础的"指针"。 例如:
/********** example 1 **********/declare @au_id char( 11 ) set rowcount 0 select * into #mytemp from authors set rowcount 1 select @au_id = au_id from #mytemp while @@rowcount <> 0 begin set rowcount 0 select * from #mytemp where au_id = @au_id delete #mytemp where au_id = @au_id set rowcount 1 select @au_id = au_id from #mytemp<BR/> end set rowcount 0 第二种方法是使用 min 函数,以表格一行的"遍"一次。 此方法捕捉的添加后该存储的过程开始执行,假设新行具有一个唯一的标识符大于正在处理在查询中的当前行新行。 例如: /************ example 2 **********/declare @au_id char( 11 ) select @au_id = min( au_id ) from authors while @au_id is not null begin select * from authors where au_id = @au_id select @au_id = min( au_id ) from authors where au_id > @au_id end 备注 : 1 和 2 两个示例假定一个唯一的标识符存在对于源表中的每一行。 在某些情况下,可能存在没有唯一标识符。 如果是这种情况,您可以修改要使用新创建的键列 临时 表方法。 例如: /********** example 3 **********/ set rowcount 0 select NULL mykey, * into #mytemp from authors set rowcount 1 update #mytemp set mykey = 1 while @@rowcount > 0 begin set rowcount 0 select * from #mytemp where mykey = 1 delete #mytemp where mykey = 1 set rowcount 1 update #mytemp set mykey = 1 end set rowcount 0 原文:/article/6158074.html
相关文章推荐
- 如何遍历一个结果集在 SQL Server 中使用 Transact-SQL
- 如何遍历一个结果集在 SQL Server 中使用 Transact-SQL
- 在 SQL Server 中使用 Transact-SQL如何遍历一个结果集
- 在 SQL Server 中使用 Transact-SQL如何遍历一个结果集
- 如何通过 Transact-SQL SQL Server 中遍历一个结果集
- 使用Transact-SQL 遍历结果集 (转)
- 在SQL Server中sqlserver,access,excel之间数据如何使用sql语句直接操作
- 如何使用DataGrip导出整个数据库到一个.sql文件
- Transact-SQL 示例 - 如何获取一个变量的数据类型
- 在SQL Server中使用SQL语句查询一个存储过程被其它所有的存储过程引用的存储过程名
- 使用Transact-SQL 遍历结果集
- 【SQL Server技巧篇】如何使用Transact-SQL脚本语言导入Excel表
- Transact-SQL语句遍历结果集的三种方法
- 使用 Transact-SQL 语句来循环结果集
- 数据库控件dotConnect for SQL Server使用教程:如何连接到SQL Server
- 如何测试一个SQL Server连接使用ODBC驱动程序
- ubuntu下如何使用mysql/python中如何调用sql新建一个数据库
- 一个表里有多个字段需要同时使用字典表进行关联显示,如何写sql查询语句
- 如何修改 SQL Server 代理主作业 (Transact-SQL)
- SQL2005 中 pivot() 的使用 如何通过一个SQL语句变成竖表