Oracle dynamic SQL

Source: Internet
Author: User

Dynamic SQL syntax

Only when running can Oracle check whether its format is correct

The INTO and USING clauses are optional.

If the SQL statement is a query statement, we can use the INTO clause.

The INTO statement is used to receive the record value selected by the SELECT statement. It can be a variable sequence or a record-type variable, that is, a record-type variable.

The sequence of this variable corresponds to the sequence of the record values in the query result set.

If parameters need to be dynamically determined, we can use the USING clause.

Dynamic table Creation

Example

  1. -- Dynamic SQL statement
  2. Begin
  3. ExecuteImmediate'Create table bonus (id number, ant number )';
  4. End;
  5. -- Dynamically query users' phone numbers
  6.  Declare
  7. SQL _stmt varchar2 (200 );-- Store query statements
  8. Emp_id number (10): ='& Emp_id';
  9. Emp_rec employees % rowtype;
  10.  Begin
  11. SQL _stmt: ='Select * from employees where id =: id';
  12. ExecuteImmediate SQL _stmtIntoEmp_rec using emp_id;
  13. Dbms_output.put_line (emp_rec.phone );
  14.  End;
  15. ">-- Dynamic insert
  16.  Declare
  17. SQL _stmtVarchar(200 );
  18. Emp_id number (10): ='& Emp_id';
  19. Emp_rec employees % rowtype;
  20.  Begin
  21. SQL _stmt: ='Insert into employees (id) values (: id )';
  22. ExecuteImmediate SQL _stmt using emp_id;
  23.  End;

The execute immediate statement can only be executed to return one or no results. If you want to write an SQL statement that returns multiple rows, you must use the REF dynamic cursor.

Example:

  1. -- Dynamic SQL and Dynamic Cursor
  2.  Declare
  3. E_id number (10 );
  4. E_name varchar2 (50 );
  5. S_salary number (8 );
  6. Type c_typeIsRefCursor;
  7. Cur c_type;
  8. P_salaty number: ='& P_id';
  9.  Begin
  10. OpenCurFor
  11. 'SelectE. id, e.Name, S. salaryvalueFromEmployees e, salary s
  12. WhereE. id = s. employeeidAndS. salaryvalue>: salOrder ByIdAsc'
  13. Using p_salry;
  14. Dbms_output.put_line ('Salary higher'| P_salary |'Employees :');
  15. Loop
  16. FetchCurIntoE_id, e_name, s_salary;
  17. ExitWhenCur % notfound;
  18. Dbms_output.put_line ('No :'| E_id |'Name :'| E.Name|'Pause'| S_salary );
  19. EndLoop;
  20. CloseCur;
  21.  End;

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.