数据库---存储过程

--验证存储过程是否正确时,需要用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;


猜你喜欢

转载自blog.51cto.com/13693838/2104068