plsql動態語句

2021-05-21 20:52:22 字數 1928 閱讀 3330

--   select * from std_mes_shipdata ; 

--set serveroutput on  ;

做乙個儲存,以後參考:

create or replace package xxoutput

as type mycrs is ref cursor ;

procedure xxfnsoutput ( cur out mycrs ) ;

end ;

create or replace package body xxoutput

as procedure xxfnsoutput ( cur out mycrs )

is tmpsqlstring varchar2(4000) ;

iloopn number ;

iloopdayth number ;

idaythnow number ;     

begin    

iloopn := 1 ;

iloopdayth :=1 ;

idaythnow := to_number( substr( to_char ( sysdate,'yyyymmdd' ),7,2 ) ) ;

tmpsqlstring := ' select productsquare ' ;

--  動態sql :

while iloopdayth <= idaythnow

loop

tmpsqlstring := tmpsqlstring || ', sum( decode ( dayn, '''||to_char(  trunc(sysdate,'mm')+(iloopdayth-1),'yyyymmdd' )

||''',qty,0)) as '||'d'

||to_char(  trunc(sysdate,'mm')+(iloopdayth-1),'yyyymmdd' ) ;

iloopdayth := iloopdayth +1 ;

end loop ;       

--  附 動態sql的源表:   

tmpsqlstring := tmpsqlstring || ' from ( select case substr(trim(product_id),1,1)

when ''f'' then ''4''

when ''w'' then ''5''

when ''m'' then ''6''

end  as productsquare,         

substr(trim(ship_date),1,8) as dayn,

sum(qty) as qty

from   std_mes_shipdata 

where  substr(trim(ship_date),1,6) = substr( to_char( trunc( sysdate,''mm''),''yyyymmdd''  ),1,6) 

group by substr(trim(product_id),1,1) ,  substr(trim(ship_date),1,8)

order by  substr(trim(ship_date),1,8)  ) drtbl group by productsquare '  ;

--  列印最後的sql 語句:     

/*while iloopn<=length( trim( tmpsqlstring )  )

loop

dbms_output.put_line( substr( trim( tmpsqlstring ) ,iloopn, 150 ) )  ;

iloopn := iloopn +150 ;         

end loop ;   

*/open cur for tmpsqlstring ;

end ;

end ;

PL SQL控制語句

本節要點 l 迴圈結構控制語句 pl sql既然是面向過程的程式語言,那麼它就有針對邏輯的控制語句,這些語句在日常的pl sql程式設計中起著很重要的作用,可以完成業務邏輯的框架部分。下面就來介紹pl sql的邏輯控制語句。1選擇結構控制語句 1.1if條件控制語句 條件控制語句就是根據當前某個引數...

PL SQL 迴圈控制語句

判斷語句 if.else declare v age number not null 50 beginif0 v age and v age 18 then dbms output.put line 兒童 elsif 18 v age and v age 30 then dbms output.pu...

PLSQL基本控制語句

根據員工號,查詢員工薪水 declare v empno emp.empno type v sal emp.sal type begin v empno 7369 select sal into v sal from emp where empno v empno dbms output.put l...