Learning notes-static SQL and dynamic SQL

Source: Internet
Author: User

One: Static SQL

SQL statements have been written (determined at compile time) when writing the PL-SQL program

DDL and Session control statements cannot be used directly in PL/SQL

Two: Dynamic SQL

SQL statements are not yet deterministic when writing a PL * + program

Not compiled, dynamically determined at execution time

Need to dynamically determine SQL statements based on user input parameters

Resolving issues that do not support DDL statements in PL/SQL

Dynamic SQL Syntax:

EXECUTE IMMEDIATE ' DDL,DML statement '---SQL statement is in the form of a string, if there are parameters in the SQL statement, you can: use the parameter name

[into〈 variable sequence]--to accept the record value selected by the SELECT statement

[using〈 parameter sequence];--for binding input parameter variables

--The Execute immediate statement can only execute a row that is returned or does not have an SQL statement

You can use a ref dynamic cursor when you need to execute an SQL statement that returns multiple rows

Dynamically creating SQL statements that return multiple rows

Grammar:

Declaration is the same as a normal REF cursor, except that it is opened differently

OPEN REF CURSOR name for ' SQL statement string '

[using〈 parameter column];

Example----Dynamic SQL-------------------------------

DECLARE
V_ID Number: = &vid;
V_name VARCHAR2 (): = ' &vname ';
V_sex VARCHAR2 (): = ' &sex ';
Insert_sql VARCHAR2 (+): = ' INSERT into student values (: 1,:2,:3) '; ---: 1 is a bound variable and is a placeholder in dynamic SQL
BEGIN
EXECUTE IMMEDIATE insert_sql USING v_id,v_name,v_sex; ----EXECUTE immediate can be added directly after the ' string SQL statement '
END;

Example 2----------ref dynamic cursor---------

DECLARE
TYPE ref_cur is ref CURSOR;
RC Ref_cur;
Emp_row Dept%rowtype;
Select_sql VARCHAR2 (+): = ' select * FROM dept ';
BEGIN
OPEN RC for Select_sql;
LOOP
FETCH RC into Emp_row;
EXIT when Rc%notfound;
--dbms_output.put_line (rc%rowcount| | ' ' | | emp_row.empno| | ' ' | | emp_row.ename| | ' ' | | Emp_row.sal);
Dbms_output.put_line (emp_row.deptno| | Emp_row.dname);
END LOOP;
CLOSE RC;
END;

Learning notes-static SQL and dynamic SQL

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.