摘要:本文讲解了 SQL Server 数据库中Transact的使用,希望本文可以帮助您对 SQL Server 数据库的理解更加深入。
使用 Transact-SQL 语句来循环结果集
there are can use to iterate through result set by using Transact-SQL statements three methods。
一种方法是使用临时表。 使用此方法,您创建初始 SELECT 语句的"快照"并将其用作基础的"指针"。 例如:
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 end set rowcount 0
第二种方法是使用 min 函数,以表格一行的"遍"一次。 此方法捕捉的添加后该存储的过程开始执行,假设新行具有一个唯一的标识符大于正在处理在查询中的当前行新行。 例如:
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 两个示例假定一个唯一的标识符存在对于源表中的每一行。 在某些情况下,可能存在没有唯一标识符。 如果是这种情况,您可以修改要使用新创建的键列 临时 表方法。 例如:
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
本文由职坐标整理发布,关注SQL Server频道,学习更多的SQL知识!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号