db2 存儲過程 語法 及結果集查詢

第一次用存儲過程,關於處理待辦的,不知道怎麼執行和傳參數 給存儲過程 ,其實就一句話很簡單。數據庫

@call PRC_MISSIONLIST_QUERY('27020214', '27040000', '27040900'); 或者call PRC_MISSIONLIST_QUERY('27020214', '27040000', '27040900');ssh

其中的三個參數其實就是存儲過程當中的in 參數。fetch

存儲過程很複雜慢慢看。優化

(IN | OUT | INOUT parameter-name data-type,...) :傳入參數
IN:輸入參數
OUT:輸出參數
INOUT:做爲輸入輸出參數
parameter-name:參數名字,在此存儲過程當中惟一的標識符。
data-type:參數類型,能夠接收SQL類型和建立的表。不支持LONG VARCHAR, LONG VARGRAPHIC, DATALINK, REFERENCE和用戶自定義類型。 編碼

 

SPECIFIC specific-name:惟一的特定名稱(別名),能夠用存儲過程名代替,這個特定名稱用於dorp存儲過程,或者給存儲過程添加註視用,但不能調用存儲過程。若是不指定,則數據庫會自動生成一個yymmddhhmmsshhn時間戳的名字。推薦給出別名。 url

 

DYNAMIC RESULT SETS integer:指定存儲過程返回結果的最大數量。存儲過程當中雖然沒有return語句,可是卻能返回結果集。spa

 

CONTAINS SQL, READS SQL DATA, MODIFIES SQL DATA: 指定存儲過程當中的SQL訪問級別
CONTAINS SQL: 表示存儲過程能夠執行中,既不可讀取 SQL 數據,也不可修改 SQL 數據。
READS SQL DATA: 表示存儲過程能夠執行中,可讀取SQL,但不可修改 SQL 數據。
MODIFIES SQL DATA: 表示存儲過程能夠執行任何 SQL 語句。能夠對數據庫中的數據進行增長、刪除和修改。 code

DETERMINISTIC or NOT DETERMINISTIC:表示存儲過程是動態或者非動態的。動態的返回的值是不肯定的。非動態的存儲過程每次執行返回的值是相同的。 blog

CALLED ON NULL INPUT:表示能夠調用存儲過程而無論任何的輸入參數是否爲NULL,而且,任何的OUT或者INOUT參數能夠返回一個NULL或者非空值。檢驗參數是否爲NULL是在過程當中進行的。

INHERIT SPECIAL REGISTERS:表示繼承專用寄存器。

OLD SAVEPOINT LEVEL or NEW SAVEPOINT LEVEL:創建存儲點。OLD SAVEPOINT LEVEL是默認的存儲點。

LANGUAGE SQL:指定程序的主體用的是SQL語言。

EXTERNAL ACTION or NO EXTERNAL ACTION:表示存儲過程是否執行一些改變理數據庫狀態的活動,而不經過數據庫管理器管。默認是 EXTERNAL ACTION。若是指定爲NO EXTERNAL ACTION ,則數據庫會肯定最最佳優化方案。

PARAMETER CCSID:指定全部輸出字符串數據的編碼,默認爲UNICODE編碼數據庫爲PARAMETER CCSID UNICODE ,其餘的數據庫默認爲PARAMETER CCSID 3 ASCII。

SQL-procedure-body:存儲過程的主體 繼承

----------SQL-variable-declaration:變量聲明

----------Variable declaration statement代碼 收藏代碼
     DECLARE variable-name data-type [DEFAULT value]  
 
variable assign statement:變量賦值語句 常見的變量賦值語句有:SET,VALUES INTO,SELECT INTO,FETCH語句
Set statement代碼  收藏代碼
  1. SET variable-name = value   
  2. SET variable-name = (SELECT statement)  
logic statement:邏輯語句
常見的邏輯語句有:IF,CASE,FOR,WHILE,ITERATE,LEAVE,LOOP,REPEAT,GOTO語句
 
RETURN,OPEN,CLOSE,CALL
說明:通常要使用遊標以前,先declare,再open,這樣遊標才能fetch,最後close
Close statement代碼  收藏代碼
  1. >>--CLOSE--cursor-name-----------------------------------------><  
  >>--CLOSE--cursor-name-----------------------------------------><
1.說明:關閉遊標
Call statement代碼  收藏代碼
  1. >>--CALL--procedure-name(parameter list)-----------------------><  
.說明:調用其它的存儲過程,通常IN類型的參數用戶傳值,OUT類型的參數用"?"代替,如   建立一個求兩整數和的存儲過程:    CAREATE PROCEDURE SUM(IN p_one INT, IN p_two INT, OUT p_result INT)    ......   調用這個存儲過程:    CALL SUM(2,4,?)或CALL <user-schema>.SUM(2,4,?) 刪除一個已建立的存儲過程,語法:
Drop procedure statement代碼 收藏代碼
  1. DROP SPECIFIC PROCEDURE specific-name    
  2. DROP PROCEDURE procedure-name(parameter list)  
DROP SPECIFIC PROCEDURE specific-name 
DROP PROCEDURE procedure-name(parameter list)
example:   DROP SPECIFIC PROCEDURE SUM_ab;(假設上面SUM存儲過程的SPECIFIC爲SUM_ab)   DROP PROCEDURE SUM(INT,INT,INT)
 

CREATE PROCEDURE "TRAINING"."PRC_MISSIONLIST_QUERY" (    

IN "V_EMPLOYEEID" VARCHAR(60),   

IN "V_UPPERUNIT2" VARCHAR(60),   

IN "V_UPPERUNIT3" VARCHAR(60) )

SPECIFIC "SQL121009165611600"  

DYNAMIC RESULT SETS 2  

LANGUAGE SQL  

NOT DETERMINISTIC  

NO EXTERNAL ACTION  

READS SQL DATA  

OLD SAVEPOINT LEVEL

begin

declare V_SORT varchar(4) default '';

declare v_isCourseAdmain integer default 0; --其它 待辦 信息

other:begin  

DECLARE other_list CURSOR WITH RETURN TO CALLER for   /* 考試待辦信息*/  

select tm.seqcode flowid,'部門考試('||tet.EVALUATE_NAME||')' topic ,tm.created_time limitdate,tet.EVALUATE_SEQCODE flowNum,tm.end_time endDate,    

'UTY' flowtype,'3' coalitionflag, '學員' upuserrole,0 missionlistId,(tm.created_time+ 2 day) endDate,    

case when (tm.created_time + 2 day) <= current timestamp then 1 else 0 end as status    

from

TRAINING.TRAINING_EVALUATE tet    

inner join TRAINING.TRAINING_MAIN tm on tm.seqcode=tet.TRAINING_SEQCODE    

inner join TRAINING.TRAINING_PEPINFO tpi on tpi.TRAINING_SEQCODE=tm.seqcode    

where

tm.del_time is null and (tm.STATE='0' or tm.state='3')

and tm.kind='5' and tpi.pep_state='1'    

and (tpi.state='0' or tpi.state='2' or tpi.state='3')

and tpi.join_flag='N'

and tpi.PEP_ID=V_EMPLOYEEID    

and current timestamp between tm.start_time and tm.end_time;        

open other_list;

end other;

/*判斷 用戶是否有 課程管理員 權限*/

select count(er.SEQCODE) into v_isCourseAdmain from

TRAINING.EMPLOYEE_ROLEINFO er  

inner join TRAINING.ROLEINFO rlf on rlf.SEQCODE = er.EMPLOYEE_ROLE  

where rlf.ROLE_NAME='課程管理員' and rlf.AVI_FLAG='1' and rlf.DEL_TIME is null   and er.EMPLOYEE_ID =V_EMPLOYEEID and er.CROUSE_SORT_CODE is not null ;  

if v_isCourseAdmain > 0 then  

select max(er.CROUSE_SORT_CODE) into V_SORT from EMPLOYEE_ROLEINFO er where er.EMPLOYEE_ID = V_EMPLOYEEID;

end if;

/*查詢 用戶 待辦*/

cur:begin    

DECLARE missionlist_list CURSOR WITH RETURN TO CALLER for    

select ml.FLOWOID flowid,ml.MISSIONLISTOID missionlistid, ml.FLOWNUM flownum,ml.USERROLEOID userroleoid,ml.UPUSERROLE upuserrole,       ml.TOPIC topic,ml.USEPAGEURL url,ml.FLOWFLAG flowtype,ml.UPPERNAME uppername,ml.MISSIONDATE limitdate,(ml.MISSIONDATE + ml.DAYS_NUM day) endDate,      

case when (ml.MISSIONDATE + ml.DAYS_NUM day)<= current date then 1 else 0 end as status, ml.COALITION_FLAG coalitionflag,value(ri.seqcode,5) rolenum    

from MISSIONLIST_VIEW ml    

left join roleinfo ri on ml.USERROLEOID = ri.ROLE_NAME     

where   (ml.useroid = V_EMPLOYEEID OR ml.USEROID = '0')            

AND (ml.upperunit2 = V_UPPERUNIT2 or ml.pepkind = '1')            

AND (                 (                   '' <> concat(V_UPPERUNIT3,'')                  

AND  concat(V_UPPERUNIT3,'') not in ('27000043','27000066')                  

AND (ml.upperunit3 = V_UPPERUNIT3 or ml.pepkind != '3')                 )                 OR                 1=1             )            

AND (

EXISTS (                    

                select eri.EMPLOYEE_ROLE from EMPLOYEE_ROLEINFO eri

where eri.EMPLOYEE_ID=V_EMPLOYEEID AND ri.SEQCODE = eri.EMPLOYEE_ROLE AND  

(eri.UNIT_TWO_CODE=V_UPPERUNIT2 OR  (eri.UNIT_THREE_CODE=V_UPPERUNIT3 AND eri.UNIT_TWO_CODE=V_UPPERUNIT2))  )   OR ml.USERROLEOID = '學員'  )     AND

NOT EXISTS(

select 1 from Missionlist_pep b where b.ml_seqcode = ml.Missionlistoid AND ( (ml.USERROLEOID='課程管理員'                          

AND ml.FLOWFLAG NOT IN ('NCLF')   

AND EXISTS(

select employee_id from EMPLOYEE_ROLEINFO dd where dd.EMPLOYEE_ID=b.PEP_CODE

AND dd.CROUSE_SORT_CODE= V_SORT ) )  

OR   ( 

/*ml.USERROLEOID <> '課程管理員'  AND   (

*/  b.pep_code = V_EMPLOYEEID  OR    (b.PEP_CODE = '0' AND (

b.unit_twocode = V_UPPERUNIT2 ||','|| V_UPPERUNIT3 OR b.unit_twocode = V_UPPERUNIT2 ||','))  ) )AND ml.FLOWFLAG not in ('CP','RGC') )    AND            

NOT EXISTS(

SELECT b.unit_twocode FROM Missionlist_pep b     

INNER JOIN Missionlist c ON b.ML_SEQCODE = c.Missionlistoid                    

WHERE c.userroleoid = '二級培訓管理員' AND c.display_flag = '1'  AND c.sparenum = 1 AND ml.USERROLEOID = '學員'

and ml.flowflag=c.flowflag  AND ml.flowoid = c.flowoid AND b.UNIT_TWOCODE=V_UPPERUNIT2||',' )

AND( (   ml.OBJ_UNITS is NOT NULL  AND ( ( ml.OBJ_UNITS like concat('%',concat(V_UPPERUNIT2,'%'))

AND ml.USERROLEOID<>'二級培訓管理員' AND ml.PEPKIND ='1'

-- AND ml.OBJ_UNITS like '%27220000%'    )    OR    (    ml.USERROLEOID ='二級培訓管理員'     AND ml.PEPKIND ='1' AND ml.OBJ_UNITS like concat('%',concat(V_UPPERUNIT2,'%'))   AND EXISTS (     select aa.OBJ_UNITS from MISSIONLIST aa  

  left join MISSIONLIST bb on aa.BACKMISOID=bb.MISSIONLISTOID    

where  EXISTS (  select bbb.BACKMISOID from MISSIONLIST aaa      left join MISSIONLIST bbb on aaa.BACKMISOID=bbb.MISSIONLISTOID     where bbb.ISBACK=1 AND bbb.UPUSERROLE='課程管理員' AND aaa.MISSIONLISTOID=ml.MISSIONLISTOID AND bb.BACKMISOID=bbb.BACKMISOID   ) AND aa.OBJ_UNITS like concat('%',concat(V_UPPERUNIT2,'%'))   ) )

OR(

ml.flowflag = 'CP'  AND ml.OBJ_UNITS like concat('%',concat(V_UPPERUNIT2,'%')) AND ml.PEPKIND ='1'  )  OR  (  ml.pepkind ='2' AND ml.obj_units like concat('%',concat(V_UPPERUNIT3,'%'))   )    )  )   OR ml.obj_units is null OR (ml.USERROLEOID='系統管理員' AND ml.FLOWNUM=10)             )       OPEN missionlist_list ;      --   commit;     end cur; end

相關文章
相關標籤/搜索