<script>function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>要对一个表的每一行做出比较复杂的操作,一般会想到用游标,今天想到使用SQL2005中的新函数ROW_NUMBER()和while循环来对每一行执行操作。示例代码如下:
selectDepartment_NoasdepartmentNo,ROW_NUMBER()OVER(ORDERBYDepartment_No)ASrowNumberinto#depTemp--建立临时表
fromdepartments

declare@maxint--获得最大的rowNumber
select@max=max(rownumber)
from#depTemp

declare@rowNoint
set@rowNo=1
while@rowNo<=@max--对每一个rowNumber进行循环操作
begin
--这儿对每一行要进行的操作的代码
set@rowNo=@rowNo+1
end

droptable#depTemp--清除临时表
selectDepartment_NoasdepartmentNo,ROW_NUMBER()OVER(ORDERBYDepartment_No)ASrowNumberinto#depTemp--建立临时表
fromdepartments
declare@maxint--获得最大的rowNumber
select@max=max(rownumber)
from#depTemp
declare@rowNoint
set@rowNo=1
while@rowNo<=@max--对每一个rowNumber进行循环操作
begin
--这儿对每一行要进行的操作的代码
set@rowNo=@rowNo+1
end
droptable#depTemp--清除临时表
本文介绍了一种在SQL Server 2005中使用ROW_NUMBER()函数结合while循环处理表中每一行数据的方法,并提供了一个具体的示例代码,展示了如何通过创建临时表并对其进行循环操作来实现复杂的数据处理。

被折叠的 条评论
为什么被折叠?



