小编Jac*_*cob的帖子

为什么选择顶级查询和临时表而不是循环游标?

当谈到在 T-SQL 中循环遍历大量数据时,我在 Internet 上看到的大多数示例都使用这样的模式:

declare @someVariable int
declare @remainingRows int

select someColumn from someTables into #someTempTable

select @remainingRows = count(*) from #someTempTable
while @remainingRows > 0 begin

    select top 1 @someVariable = someColumn from #someTempTable

    -- Do stuff

    delete top 1 from #someTempTable

    select @remainingRows = count(*) from #someTempTable
end
Run Code Online (Sandbox Code Playgroud)

这似乎比使用这样的游标的示例更常见:

declare @someVariable int

select someColumn from someTables into #someTempTable

declare @someCursor cursor for select someColumn from #someTempTable
open @someCursor
fetch next @someVariable from @someCursor

while @@fetch_status = 0 …
Run Code Online (Sandbox Code Playgroud)

sql-server t-sql cursors

6
推荐指数
1
解决办法
8520
查看次数

标签 统计

cursors ×1

sql-server ×1

t-sql ×1