如何遍历一个结果集在 SQL Server 中使用 Transact-SQL
来源:互联网 发布:2017继续教育网络效应 编辑:程序博客网 时间:2024/05/16 01:40
使用 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
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
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
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
阅读全文
0 0
- 在 SQL Server 中使用 Transact-SQL如何遍历一个结果集
- 如何遍历一个结果集在 SQL Server 中使用 Transact-SQL
- 如何通过 Transact-SQL SQL Server 中遍历一个结果集
- 如何通过使用 TRANSACT-SQL SQL Server 中循环访问一结果集
- 使用Transact-SQL语句遍历结果集
- Transact-SQL语句遍历结果集的三种方法
- Transact-SQL语句遍历结果集的三种方法
- Sql Server 中如何循环遍历查询结果
- Sql Server 中如何循环遍历查询结果
- 在 Transact-SQL 中使用 TRY...CATCH
- 使用 Transact-SQL 语句来循环结果集
- 如何在SQL Server中使用触发器
- 如何在SQL Server中使用触发器
- 如何在SQL Server中使用触发器
- 在SQL Server 2012中如何使用分组集
- 如何在SQL Server查询语句(Select)中检索存储过程(Store Procedure)的结果集?(
- 在 Transact-SQL 中使用 TRY...CATCH XACT_STATE
- 【SQL Server技巧篇】如何使用Transact-SQL脚本语言导入Excel表
- eclipse启动后进度卡在AppXray Resource Change Handler
- Mongodb删除数据库操作
- codeforces 818D Multicolored Cars
- 【数组】判断数组为空还是长度为0好?
- lambertian、diffuse、matte的关系
- 如何遍历一个结果集在 SQL Server 中使用 Transact-SQL
- NS2中Mac802_11::recv(Packet *p, Handler *h )函数解析
- 五.BeautifulSoup大众点评爬取店铺信息,存储到mongodb
- service或controller报空指针异常
- LINUX文件系统
- Java数据结构和算法(1)--自定义一个数组类和动态数组类
- FFmpeg再学习 -- Windows下安装说明
- 波兰表示法与逆波兰表示法
- 学无止境!!!