Oracle EXECUTE IMMEDIATE语句

1.当执行SQL语句时,不要用分号,当执行PL/SQL块时,在其尾部用分号
2.不支持返回多行的查询,使用临时表来存储记录或者用REF cursors.

sample:

1. USING 子句
  declare
    starff varchar2(10) := 'TS00001';
    table_name varchar2(12) := 'TV00001_VIOL';
    periord number := 5;
      begin
          execute immediate 'insert into '||table_name ||' values (:1, ''ORGCODE'',:2)'
           using starff, sysdate+periord;
       commit;
2. INTO子句
  declare
     l_cnt     varchar2(20);
  begin
     execute immediate 'select count(1) from emp'
       into l_cnt;
     dbms_output.put_line(l_cnt);
  end;
3. 传递并检索值
  declare
     l_dept     pls_integer := 20;
     l_nam      varchar2(20);
     l_loc      varchar2(20);
  begin
     execute immediate 'select dname, loc from dept where deptno = :1'
       into l_nam, l_loc
       using l_dept ;
  end;

你可能感兴趣的:(数据库)