Execute immediate in Oracle

Source: Internet
Author: User
Tags oracle cursor

Execute immediate replaces the DBMS_ SQL package in Oracle. The following describes how to use EXECUTE IMMEDIATE in Oracle for your reference.

It parses and immediately executes dynamic SQL statements or PL/SQL blocks created when they are not running. the performance of dynamic creation and execution of SQL statements is advanced. The goal of EXECUTE IMMEDIATE is to reduce the enterprise cost and obtain high performance, which is easier to code than before. although DBMS_ SQL is still available, EXECUTE IMMEDIATE is recommended because it gains more than the package.

-- Tips

1. execute immediate will not commit a DML transaction execution and should be committed explicitly
If you use execute immediate to process DML commands,
Before the execution is complete, you need to submit it explicitly or as part of execute immediate.
If you use execute immediate to process DDL commands, it submits all previously changed data

2. multi-row queries are not supported. This interaction uses a temporary table to store records (see the example below) or REF cursors.

3. Do not use semicolons when executing SQL statements. When executing PL/SQL blocks, use semicolons at the end.

4. These features are not covered in detail in the Oracle manual.
The following example shows all possible ways to use Execute immediate.

5. For Forms developers, this function is not available for Forms 6i in PL/SQL 8.0.6.3.

Execute immediate -- usage example

1. Run DDL statements in PL/SQL

 
 
  1. begin   
  2.    execute immediate 'set role all';   
  3. end;  

2. Pass values to dynamic statements (USING clause)

 
 
  1. declare   
  2.    l_depnam varchar2(20) := 'testing';   
  3.    l_loc     varchar2(10) := 'Dubai';   
  4.    begin   
  5.    execute immediate 'insert into dept values   (:1, :2, :3)'   
  6.      using 50, l_depnam, l_loc;   
  7.    commit;   
  8. end;  

3. Retrieve values from dynamic statements (INTO clause)

 
 
  1. declare   
  2.    l_cnt     varchar2(20);   
  3. begin   
  4.    execute immediate 'select count(1) from emp'   
  5.      into l_cnt;   
  6. dbms_output.put_line(l_cnt);   
  7. end;  

4. Dynamic call routine. The variable parameters used in the routine must specify the parameter type.
Struct is considered as the IN type. Other types must be explicitly specified.

 
 
  1. declare   
  2.    l_routin    varchar2(100) := 'gen2161.get_rowcnt';   
  3.    l_tblnam    varchar2(20) := 'emp';   
  4.    l_cnt       number;   
  5.    l_status    varchar2(200);   
  6. begin   
  7. execute immediate 'begin ' || l_routin || '(:2, :3, :4); end;'   
  8.      using in l_tblnam, out l_cnt, in out l_status;   
  9. if l_status != 'OK' then   
  10.       dbms_output.put_line('error');   
  11. end if;   
  12. end;  

5. Pass the return value to the PL/SQL record type. The % rowtype variable is also available.

 
 
  1. declare   
  2. type empdtlrec is record (empno   number(4),   
  3.                             ename   varchar2(20),   
  4.                             deptno   number(2));   
  5. empdtl empdtlrec;   
  6. begin   
  7. execute immediate 'select empno, ename, deptno ' ||   
  8.                     'from emp where empno = 7934'   
  9. into empdtl;   
  10. end;  

6. Pass and retrieve the value. INTO clause before the USING clause

 
 
  1. declare   
  2.    l_dept     pls_integer := 20;   
  3.    l_nam      varchar2(20);   
  4.    l_loc      varchar2(20);   
  5. begin   
  6.    execute immediate 'select dname, loc from dept where deptno = :1'   
  7.      into l_nam, l_loc   
  8.      using l_dept ;   
  9. end;  

7. multi-row query option. This option is used to fill the temporary table with the insert statement,
You can also use REF cursors to correct this defect by using temporary tables for further processing.

 
 
  1. declare   
  2.    l_sal    pls_integer := 2000;   
  3. begin   
  4. execute immediate 'insert into temp(empno, ename) ' ||   
  5.             '           select empno, ename from emp ' ||   
  6.             '           where   sal > :1'   
  7. using l_sal;   
  8. commit;   
  9. end;  

For Processing dynamic statements, execute immediate is easier and more efficient than previously used.
When you want to execute dynamic statements, it is more important to properly handle exceptions. You should focus on capturing all possible exceptions.

Oracle authorization statement

Oracle user syntax Modification

Three methods for Oracle Authentication

Oracle Default User Password Problems

Common Oracle cursor attributes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.