Home  >  Article  >  Database  >  在SQL 2005中实现循环每一行做一定的操作_MySQL

在SQL 2005中实现循环每一行做一定的操作_MySQL

WBOY
WBOYOriginal
2016-06-01 13:51:07932browse

bitsCN.com

    在SQL 2005中实现循环每一行做一定的操作
2009-03-05 16:00   来源:不详   作者:佚名 RSS复制链接打印核心提示:如果我们想对一个表的每一行做出比较复杂的操作,大多会想到用游标,本文中,我们将换一种思路,用SQL Server 2005中的新函数ROW_NUMBER()和while循环来对每一行执行操作。详细的示例代码如下:

如果我们想对一个表的每一行做出比较复杂的操作,大多会想到用游标,本文中,我们将换一种思路,用SQL Server 2005中的新函数ROW_NUMBER()和while循环来对每一行执行操作。详细的示例代码如下:
 

      select Department_No as departmentNo,ROW_NUMBER()

OVER(ORDER BY Department_No) AS rowNumber into

#depTemp--建立临时表
from departments

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

declare @rowNo int
set @rowNo=1
while @rowNo--用来对每一个rowNumber来进行循环操作
begin
--此处对每一行要进行的操作的代码
set @rowNo=@rowNo+1
end
drop table #depTemp--清除临时表

bitsCN.com
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn