儲存過程,游標,迴圈,臨時表

2021-05-22 22:43:01 字數 3304 閱讀 3884

create procedure hr_attabn_qry2

@d_date nvarchar(10)=null,

@deptno nvarchar(1000)=null

as begin

declare @sql nvarchar(1000)

--建立臨時表

create table #mytemptable (deptno nvarchar(10),

deptname nvarchar(10),

empno nvarchar(10),

cname nvarchar(10),

abn_reason nvarchar(200),

abn_in nvarchar(10),

abn_out nvarchar(10),

patro_type nvarchar(10)

) create table #mytemptable2 (deptno nvarchar(10),

deptname nvarchar(10),

empno nvarchar(10),

cname nvarchar(10),

abn_reason nvarchar(200),

abn_in nvarchar(10),

abn_out nvarchar(10),

patro_type nvarchar(10),

abn_reason_value nvarchar(2)

)set @sql=' insert into #mytemptable select

b.deptno,

dbo.getdeptmentname(b.deptno) as deptname,

a.empno,

dbo.gethrcname(a.empno)as cname,

abn_reason,

abn_in,abn_out,patro_type

from hr_attabn a,hrempm b where a. empno=b.empno and b.deptno in('+@deptno+') and d_date='''+@d_date+''' order by b.deptno, a.empno,patro_type'

print(@sql)

exec(@sql)

-- select * from #mytemptable

declare @itemcount int

--select distinct empno from #mytemptable

set @itemcount = (select   @@rowcount)

--select @itemcount as cc

/*游標*/

declare @empno nvarchar(10)

declare emp_cursor cursor for

select distinct empno from #mytemptable

open emp_cursor

fetch next from emp_cursor into @empno

while @@fetch_status = 0

begin  

declare @ptype int

set @ptype = 1001

--類別迴圈

while(@ptype <= 1004)

begin

/**2-1*/

declare @abn_reason nvarchar(10)

declare @abn_reason2 nvarchar(200)

set @abn_reason = ''

set @abn_reason2 = ''

--select abn_reason from #mytemptable where empno = @empno and patro_type = @ptype

--游標開始

declare tmp_cursor cursor for

select abn_reason from #mytemptable where empno = @empno and patro_type = @ptype

open tmp_cursor

fetch next from tmp_cursor into @abn_reason

while @@fetch_status = 0

begin   

set @abn_reason = dbo.get_abnreason_bycode(@abn_reason)

if @abn_reason2 != ''

set @abn_reason2 = @abn_reason2 + ', ' + @abn_reason

else

set @abn_reason2 = @abn_reason

fetch next from tmp_cursor into @abn_reason

endclose tmp_cursor

deallocate tmp_cursor

--游標結束

/**儲存記錄*/

if @abn_reason2 is not null and @abn_reason2 != ''

begin

set @abn_reason2 = dbo.get_patrotype_bycode(@ptype) + @abn_reason2

insert into #mytemptable2(

deptno, deptname ,empno ,cname ,abn_reason, abn_in ,abn_out ,patro_type, abn_reason_value

) select top 1 deptno, deptname ,empno ,cname ,@abn_reason2 as a, abn_in ,abn_out ,patro_type, abn_reason from #mytemptable where empno = @empno and patro_type = @ptype

end   

set @ptype = @ptype + 1

end--while end

fetch next from emp_cursor into @empno

endclose emp_cursor

deallocate emp_cursor

--游標結束

--想要的資料

select * from #mytemptable2

--刪除臨時表

drop table #mytemptable, #mytemptable2

endgo

sql儲存過程 游標 迴圈表

游標例項 利用游標迴圈表 根據userid賦值 alter procedure cursor eg1 asbegin declare a int,error int declare temp varchar 50 臨時變數,用來儲存游標值 set a 1 set error 0 begin tran...

sql server 語句 迴圈 游標 臨時表

create table tmp i sarinfor varchar 10 建立乙個臨時表 insert into tmp select a.i sailorinfoid from cert sailorletter a left join crew sailorinfo b on a.i sai...

oracle之儲存過程,臨時表,游標示例

總結如下 ddl是一種消耗資源非常大的操作,執行時盡量不要使用ddl語句,應用程式需要的臨時表應在執行之前就開始建立。不必在每個儲存過程中建立一次。臨時表總是存在的,他們作為物件存在於資料字典中,並且總是保持為空,直到有會話在其中放入資料 1 建立臨時表 sql create global temp...