數據庫---存儲過程

--驗證存儲過程是否正確時,需要用declare塊。

 declare

  i integer;

  v_sql varchar2(1000);

  -- 定義遊標

  CURSOR C_EMP IS SELECT category FROM act_hq_tem_def;

  

  begin

    

 select  count(*) into i from user_tables t where t.table_name ='SSS';

   dbms_output.put_line('I的值是:');

   dbms_output.put_line(i);

  

 if i=1 then 

   execute immediate 'drop table sss'; end if;

 IF I=0 THEN

   execute immediate '

   create table sss( 

   name number(20) )';

   end if;

  

  

   dbms_output.enable(1000000);

   

   for tab in (

     select t.guid gd, t.name  nm  from fasp_T_causer t

     )

     

     loop --loop 是開始循環, 需要結束標識loop end 。 

    

   v_sql := 'select count(*) from fasp_T_causer';

   execute immediate v_sql into i;

   

     If i > 0 then

      DBMS_OUTPUT.PUT_LINE(tab.nm || ' tab 中的name值  ' || tab.gd );

    end if;

    end loop ;

    

  

    

 --遍歷遊標

for c_row22 in c_emp loop 

    if substr(c_row22.category,-4) ='2016' then 

      dbms_output.put_line('流程類型' || c_row22.category);  

   end if;

   end loop ;  

     

 ------------------------------------------------------------  

   

   

   

    

 --創建存儲過程   

 create or replace procedure p_wf_cf(yn varchar2,yo varchar2,allds out varchar2) as

 cursor c_emp is select category from act_hq_tem_def;

 tablec1 number;

 begin

  select count(*) into tablec1 from user_tables where table_name='ACT_HI_PROCINST_' || yn;  

   allds := tablec1;

   end  p_wf_cf;




--調用存儲過程,輸出值也在函數裏傳進去。 之後可以替換輸出值,打印出來。

declare

  v_titleAVARCHAR2(20) := '2017';

    v_titlebVARCHAR2(20) := '2016';

    v_titlec11VARCHAR2(20) :='12';

begin

 p_wf_cf(v_titleA,v_titleb,v_titlec11);

  v_titlec11  :=p_wf_cf(v_titleA,v_titleb); --存儲過程中需要有return 操作,纔可用這種方式。 

  dbms_output.put_line('輸出值爲:' || v_titlec11);

 end;







--編寫存儲過程

create or replace procedure p_fasp_sc (dblink in varchar) as

  type refcursor is ref cursor;--定義遊標類型

  col_cursor refcursor;--定義遊標變量

v_deptRowfasp_T_causer%ROWTYPE ;-- 定義行類型

v_empRowfasp_T_causer%ROWTYPE ;-- 定義行類型

  tablename  varchar2(100);

  tmpsql varchar2(20000);

  exesql  varchar2(20000);

  

  v_enoemp.empno%TYPE ; -- 與emp表中empno字段類型相同的類型。

  v_resultA NUMBER NOT NULL := 100 ;  -- 定義一個非空變量v_resultA,同時賦值

  v_enameVARCHAR2(10) ;

   v_deptRowdept%ROWTYPE ;-- 裝載一行dept表中記錄

   v_date1DATE := SYSDATE ;--定義時間

  

  type emp_type is recode(

  ename emp.ename%type,

  job  emp.job%type,

  sal emp.sal%type,

  comm emp.comm%type

  );

  v_emp emp_type;--定義一個指定的複合類型變量

  

begin

  

  v_eno := &empno ;-- 由鍵盤輸入僱員編號

   SELECT * INTO v_deptRow FROM dept WHERE deptno=10 ;--查詢一行記錄放在v_deptRow中。

   

   

   

  tmpsql :='select table_name as tablename from fasp_v_usertables where table_name like ''FASP_T_GL%''';


open col_cursor for tmpsql; --打開遊標

loop 

  fetch col_cursor into tablename ; --取得遊標數據

  exit when col_cursor%notfound; --如果沒有數據則退出

  exesql := 'select count(*) from fasp_v_usertables';

  

  begin

    execute immediate exesql;

    exception

      when others then

        dbms_output.put_line('baocuole');

        raise;

       end;

end loop; 

close col_cursor;

end;




--調用存儲過程

declare

  v_titleAVARCHAR2(20) := '1212';

begin

 p_fasp_sc(v_titleA) ;

 end;

  

 

--子程序

if 。。then 。。 else

and 

or

between and

is null

not null

like


--case

 CASE v_choose

    WHEN 0 THEN

      DBMS_OUTPUT.put_line('您選擇的是第0項。') ;

    WHEN 1 THEN

      DBMS_OUTPUT.put_line('您選擇的是第1項。') ;

    ELSE

      DBMS_OUTPUT.put_line('沒有選項滿足。') ;

  END CASE ;  


RAISE --報出異常。 

COMMIT ;

--for





declare 

--類型定義

    cursor c_sal

    is 

    select empno,ename,sal from emp ;

    --定義一個遊標變量v_cinfo c_emp%ROWTYPE ,該類型爲遊標c_emp中的一行數據類型

    c_row c_sal%rowtype;

 begin

    for c_row in c_sal loop 

    dbms_output.put_line(c_row.empno||'_'||c_row.ename||'-'||c_row.sal); end loop; 

    end;


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章