Use dynamic SQL for Oracle stored procedures

Source: Internet
Author: User

I believe everyone knows about the Oracle stored procedure. The following describes how to use dynamic SQL in the Oracle stored procedure. I hope this will help you.

There are two ways to use dynamic SQL for Oracle stored procedures: DBMS_ SQL or execute immediate. We recommend that you use the latter. The test procedure is as follows:

1. DDL and DML

 
 
  1. /*** DDL ***/
  2. Begin
  3. Execute immediate 'drop table temp_1 ';
  4. Execute immediate 'create table temp_1 (name varchar2 (8 ))';
  5. End;
  6.  
  7. /*** DML ***/
  8. Declare
  9. V_1 varchar2 (8 );
  10. V_2 varchar2 (10 );
  11. Str varchar2 (50 );
  12. Begin
  13. V_1: = 'tester ';
  14. V_2: = 'beijing ';
  15. Str: = 'insert INTO test (name, address) VALUES (: 1,: 2 )';
  16. Execute immediate str USING v_1, v_2;
  17. Commit;
  18. End;

2. Return a single result

 
 
  1. Declare
  2. Str varchar2 (500 );
  3. C_1 varchar2 (10 );
  4. R_1 test % rowtype;
  5. Begin
  6. C_1: = 'tester ';
  7. Str: = 'select * from test where name =: c where rownum = 1 ';
  8. Execute immediate str into r_1 using c_1;
  9. DBMS_OUTPUT.PUT_LINE (R_1.NAME | R_1.ADDRESS );
  10. End;

3. Returned result set

 
 
  1. Create or replace package pkg_test
  2. /* Define the ref cursor type
  3. The return type is not added. The return type is weak. dynamic SQL queries are allowed,
  4. Otherwise, it is strongly typed and cannot be queried using dynamic SQL;
  5. */
  6. Type myrctype is ref cursor;
  7.  
  8. -- Function declaration
  9. Function get (intID number) return myrctype;
  10. End pkg_test;
  11. /
  12.  
  13. Create or replace package body pkg_test
  14. -- Function body
  15. Function get (intID number) return myrctype is
  16. Rc myrctype; -- defines the ref cursor variable
  17. Sqlstr varchar2 (500 );
  18. Begin
  19. If intID = 0 then
  20. -- The static test directly returns the result using the select statement.
  21. Open rc for select id, name, sex, address, postcode, birthday from
  22. Student;
  23. Else
  24. -- Assign a value to a dynamic SQL statement. Use w_id to declare that the variable is obtained from the outside.
  25. Sqlstr: = 'select id, name, sex, address, postcode, birthday from student
  26. Where id =: w_id ';
  27. -- Dynamic Test: return results using sqlstr strings and PASS Parameters using keywords
  28. Open rc for sqlstr using intid;
  29. End if;
  30.  
  31. Return rc;
  32. End get;
  33.  
  34. End pkg_test;
  35. /

Implementation of Oracle fuzzy query

Oracle Stored Procedure debugging method

Learn about the physical structure of Oracle

How to delete ORACLE archive logs

Oracle mobile redo log file

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.