不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
T-SQL中替代游标遍历结果集的方法
编辑:dnawo 日期:2013-04-25
1.借助主键字段实现方法
本方法先将数据复制到临时表,然后通过逐条读取并删除来实现替代游标遍历结果集:
说明:本方法要求Id字段为主键且唯一,若数据量比较大,应使用create table创建临时表同时设置主键,否则处理时间会很长。
2.借助标识字段实现方法
若表中有标识字段,可借助min函数实现替代游标遍历结果集:
若表中没有标识字段,可先将数据复制到临时表并创建标识字段,剩下的就和上边一样了:
本文方法在SQL Server2000/2005/2008测试通过。
本方法先将数据复制到临时表,然后通过逐条读取并删除来实现替代游标遍历结果集:
复制内容到剪贴板
程序代码

set nocount on
select * into #tab1 from Account
declare @id int, @name nvarchar(20)
select top 1 @id=Id,@name=Usn from #tab1
delete from #tab1 where Id=@id
while @@rowcount>0
begin
print @name
select top 1 @id=Id,@name=Usn from #tab1
delete from #tab1 where Id=@id
end
drop table #tab1
select * into #tab1 from Account
declare @id int, @name nvarchar(20)
select top 1 @id=Id,@name=Usn from #tab1
delete from #tab1 where Id=@id
while @@rowcount>0
begin
print @name
select top 1 @id=Id,@name=Usn from #tab1
delete from #tab1 where Id=@id
end
drop table #tab1
说明:本方法要求Id字段为主键且唯一,若数据量比较大,应使用create table创建临时表同时设置主键,否则处理时间会很长。
2.借助标识字段实现方法
若表中有标识字段,可借助min函数实现替代游标遍历结果集:
复制内容到剪贴板
程序代码

declare @id int, @name nvarchar(20)
select top 1 @id=min(Id) from Account
while @id is not null
begin
select @name=Usn from Account where Id=@id
print @name
select top 1 @id=min(Id) from Account where Id>@id
end
select top 1 @id=min(Id) from Account
while @id is not null
begin
select @name=Usn from Account where Id=@id
print @name
select top 1 @id=min(Id) from Account where Id>@id
end
若表中没有标识字段,可先将数据复制到临时表并创建标识字段,剩下的就和上边一样了:
复制内容到剪贴板
程序代码

set nocount on
select identity(int,1,1) as 'Id', Usn into #tab1 from Account
declare @id int, @name nvarchar(20)
select top 1 @id=min(Id) from #tab1
while @id is not null
begin
select @name=Usn from #tab1 where Id=@id
print @name
select top 1 @id=min(Id) from #tab1 where Id>@id
end
drop table #tab1
select identity(int,1,1) as 'Id', Usn into #tab1 from Account
declare @id int, @name nvarchar(20)
select top 1 @id=min(Id) from #tab1
while @id is not null
begin
select @name=Usn from #tab1 where Id=@id
print @name
select top 1 @id=min(Id) from #tab1 where Id>@id
end
drop table #tab1
本文方法在SQL Server2000/2005/2008测试通过。






评论: 0 | 引用: 0 | 查看次数: 3650
发表评论
请登录后再发表评论!