TSQL 不用游標的迴圈方法

2021-05-24 23:30:41 字數 814 閱讀 3486

方法一:

select * into #t1 from dbo.horsebase

declare @id int

declare @t2 table (id int)

while (exists(select horsenumber from #t1))

begin

select top 1 @id=horsenumber from #t1

insert into @t2 select horsenumber from #t1 where horsenumber=@id

delete from #t1 where horsenumber=@id

enddrop table #t1

方法二:

declare @r int

declare @number int;

declare @t2 table ( [name] nvarchar(200))

select row_number() over (order by [name] ) as number,[name] into #nn1 from dbo.tbl_entity

select @number=max(number) from #nn1

set @r=1

while (@r<=@number)

begin

insert into @t2 select [name] from #nn1 where number=@r

set @r=@r+1

enddrop table #nn1

row_number() 在分頁儲存過程中也是很好

TSQL 不用游標的迴圈方法

方法一 select into t1 from dbo.horsebase declare id int declare t2 table id int while exists select horsenumber from t1 begin select top 1 id horsenumber...

sql 游標的使用 游標FOR迴圈小例子

例子 顯示emp表所有雇員名及其工資 複製 如下 declare cursor emp cursor is select ename,sal from emp begin for emp record in emp cursor loop dbms output.put line 姓名 emp re...

使用游標 游標FOR迴圈

游標for迴圈是在pl sql塊中使用游標最簡單的方式,它簡化了對游標的處理。當使用游標for迴圈時,oracle會隱含的開啟游標,提取游標資料並關閉游標。例子 顯示emp表所有雇員名及其工資 declare cursor emp cursor isselect ename,sal from emp...