当前位置:数据库 > Oracle >>

ORACLE包内的存储过程的动态sql

ORACLE包内的存储过程的动态sql
 
--
 
---创建包
create or replace package test_pkg is
  g_public_flag varchar2(1);
  
  function test_function(p_param1 varchar2,
                         p_param2 varchar2) return varchar2;
  function  hello_function(p_param3 varchar2) return varchar2; 
  procedure ff(p1 varchar2);                     
 end;
 
 
 --包体
 create or replace package body test_pkg is
  g_private_flag varchar2(1) := 'Y'; 
 
 
  function test_function(p_param1 varchar2,
                         p_param2 varchar2) return varchar2
    is
     val varchar2(10):='hello';
    
    begin
      dbms_output.put_line('helo');
      return val;
    end ;
    
  function hello_function(p_param3 varchar2) --参数与包头中必须保持一致
    return varchar2 is
    val varchar2(10):='hello';
  begin
     dbms_output.put_line('world');
     return val;
  end;
 procedure ff(p1 varchar2) is 
    flag number; 
    pp1   VARCHAR2(10):='p';  
    p2   VARCHAR2(10):='p'; 
    p3  VARCHAR2(10):='p'; 
    p4   VARCHAR2(10):='p'; 
 begin
       select count(*)into flag from  all_all_tables where table_name='LOGTABLE1';
       dbms_output.put_line(flag);
       if(flag = 0) then
        execute immediate 
       'CREATE TABLE logtable1 (userid VARCHAR2(10), logdate VARCHAR2(10),exception_id VARCHAR2(10),exception_msg VARCHAR2(10))';
       end if;  
        execute immediate 'insert into logtable1 values (:1,:2,:3,:4)' using '1','1','2','3'; --只能动态插入
 end;
 end;   
 
----测试
declare 
begin 
   dbms_output.put_line(test_pkg.test_function('1','1'));
   dbms_output.put_line(test_pkg.hello_function('1'));
   test_pkg.ff('dd');
end;
select * from logtable1;
drop table logtable1
Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,