1. 程式人生 > >sql server 遊標的簡單用法

sql server 遊標的簡單用法

next bsp eal 用法 while begin varchar var 簡單用法

sql server遊標:

--定義遊標

declare cursor1 cursor for select ID,Name from A

--打開遊標

open cursor1

declare @id int

declare @name varchar(50)

declare @n int

declare @i int=1

set @n=(select COUNT(1) from A)

while(@i<@n)

begin

set @i=@i+1

fetch next from cursor1 into @id,@name

select @id select @name

end

--關閉遊標

close cursor1

--釋放遊標,釋放後就不能在打開遊標了

deallocate cursor1

sql server 遊標的簡單用法