SQL2005实现循环每一行做一定操作
<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--清除临时表