2012-08-08 8 views
17

レコードを一度にループするためにカーソルを作成した経験はありますか?あなたがすれば、基本的な例を私に提供することができます。ありがとうレコードをループするカーソルの作成に関するSQL Server 2008リクエストの例

+2

はい、経験があります:**それはしません!** SQLは**セットベースの**言語です** **セット** ** RBAR(row-by-agonizing-row)処理モデルを回避する** –

+2

本当に閉じるべきではない。 Q/Aのカーソルのテンプレートを持っている –

答えて

27
declare cur cursor for 
select id from tbl 
open cur 
declare @id int 
fetch next from cur into @id 
while (@@FETCH_STATUS = 0) 
begin 
    print(@id) 
    fetch next from cur into @id 
end 
close cur 
deallocate cur 

-- just replace "tbl" with your table name and "id" with your field name 
-- and do whatever you want in begin-end block (now it simply prints the id of each record) 
関連する問題