SQL Server 遍历 |
||
---|---|---|
作者:佚名 更新日期:2012年06月15日 点击: | ||
使用 Transact-SQL 语句循环结果集有三种方法使用可以通过使用 Transact-SQL 语句遍历一个结果集。一种方法是使用 temp 表。 使用这种方法您创建的初始的 SELECT 语句的"快照"并将其用作基础"指针"。 例如:
1/********** example 1 **********/
2
3 declare @au_id char( 11 )
4
5 set rowcount 0
6 select * into #mytemp from authors
7
8 set rowcount 1
9
10 select @au_id = au_id from #mytemp
11
12 while @@rowcount <> 0
13 begin
14 set rowcount 0
15 select * from #mytemp where au_id = @au_id
16 delete #mytemp where au_id = @au_id
17
18 set rowcount 1
19 select @au_id = au_id from #mytemp<BR/>
20 end
21 set rowcount 0
第二个的方法是表格的一行"遍历"每次使用 Min函数。 此方法捕获添加存储的过程开始执行之后, 假设新行必须大于当前正在处理在查询中的行的唯一标识符的新行。 例如:
1/********** example 2 **********/
2
3declare @au_id char( 11 )
4
5 select @au_id = min( au_id ) from authors
6
7 while @au_id is not null
8 begin
9 select * from authors where au_id = @au_id
10 select @au_id = min( au_id ) from authors where au_id > @au_id
11 end
注意 : 两个示例 1 和 2,则假定源表中的每个行唯一的标识符存在。 在某些情况下,可能存在没有唯一标识符。 如果是这种情况,您可以修改 temp 表方法使用新创建的键列。 例如:
1/********** example 3 **********/
|
||
Transact-SQL,遍历结果集,rowcount,YaDong网,徐亚东,亚东网 | ||
上一篇:没有了 | ||
下一篇:SQL Server数据库性能优化之SQL语句篇 |
您当前位置:YaDong网 >> 一些日志 >> SqlServer相关 >> 浏览文章