儲存過程 游標巢狀迴圈

2021-06-13 18:37:30 字數 3449 閱讀 9823

alterprocedure[dbo].[******]

asdeclare

@startnum

nvarchar (

255)

declare

@endnum

nvarchar (

255)

declare

@insurancetype

nvarchar (

255)

declare

@company_id

nvarchar(

255)

declare

@unit_id

nvarchar (

255)

declare

@insurancedivide_id

intdeclare

@certificatenoclass

nvarchar(

255)

declare

@st_nm

intdeclare

@end_nm

intdeclare

@cur_num

nvarchar(

10)declare

@currendate

nvarchar(

100)

--儲存當前時間

declare

@num_len

int--變數長度

declare

@curren_len

int--當前長度

declare

@iint

--測試臨時變數

declare

@temp

nvarchar(

8)--測試臨時變數

declare

@temp_char

nvarchar(

8)--測試臨時變數

--declare insur_cursor cursor for select startnum,endnum,insurancetype,company_id,unitid,certificatenoclass from insurancedivide where convert(int,endnum)-convert(int,startnum) -[usedamount]>0

declare

insur_cursor

cursor

forselect

startnum,endnum,insurancetype,company_id,unitid,certificatenoclass,insurancedivide_id

from

insurancedivide

where

company_id='dtbx'

andusedamountandinsurancetype

in('142','209','239','240','241','242')

begin

open

insur_cursor

fetch

next

from

insur_cursor

into

@startnum,@endnum,@insurancetype,@company_id,@unit_id,@certificatenoclass,@insurancedivide_id

while

@@fetch_status

= 0

begin

set@currendate=convert(varchar(

100),

getdate(),

23)print

@currendate

set@st_nm=convert(int,@startnum)

set@end_nm=convert(int,@endnum)

set@temp=0

set@i=

1;while

@st_nm<=@end_nm

begin

set@num_len=len(@endnum);

set@curren_len=len(@st_nm)

while

@curren_len<(@num_len)

begin

set@temp_char=convert(nvarchar(

8),@st_nm)

set@temp_char='0'+@temp_char

set@curren_len=@curren_len+1

end

declare

t_cur

cursor

forselect

count(*)

astotal

from

insurance

where

ticketid=@temp_char

andinsurancetype=@insurancetype

andcompany_id=company_id

open

t_cur

fetch

next

from

t_cur

into

@cur_num

if@cur_num=0

begin

print

@temp_char+' @cur_num '+@cur_num

insert

into

invoicenomanager(invoiceno,certificatenoclass,insurancedivide_id,suspendtype,unit_id,company_id,usedstatus,createtime,linesstatus,insurancetype)

values(@st_nm,@certificatenoclass,@insurancedivide_id,

2,@unit_id,@company_id,1,'2012-09-26',0,@insurancetype)

fetch

next

from

t_cur

into

@cur_num

endclose

t_cur

deallocate

t_cur

set@st_nm=

@st_nm+1

end

fetch

next

from

insur_cursor

into

@startnum,@endnum,@insurancetype,@company_id,@unit_id,@certificatenoclass,@insurancedivide_id

endclose

insur_cursor

deallocate

insur_cursor

end

oracle 儲存過程 游標巢狀

這是一段將裝置的gps資料的工作小時數進行統計的儲存過程,首先通過游標取出所有裝置資訊,然後對每一台裝置進行工作小時數統計,其中用到了游標巢狀 create or replace procedure cmtools.proc statistics workhour is v begindate da...

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

create procedure hr attabn qry2 d date nvarchar 10 null,deptno nvarchar 1000 null as begin declare sql nvarchar 1000 建立臨時表 create table mytemptable de...

sql儲存過程 游標 迴圈表

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