How to perform simple dynamic queries in Oracle stored procedures

Source: Internet
Author: User

Do a simple dynamic query code in a stored procedure, such as:

CREATE OR REPLACE Procedure Zxm_sb_gz_get

(P_table in Varchar2,

P_name in Varchar2,

P_value in Varchar2,

Outpara out Lntxdba.zxm_pag_cs_power.c_type

As

Begin

Declare

Wherevalue VARCHAR2 (200);

Begin

Wherevalue:=select * from | | p_table| | where | | p_name| | =|| P_value;

Open Outpara for

Wherevalue;

End

End

In general Pl/sql programming, SQL can be used directly in DML and transaction control statements, but DDL statements and system control statements cannot be used directly in Pl/sql, and in order to realize the use of DDL statements and system control statements in Pl/sql, they can be implemented by using dynamic SQL.

First we should understand what is dynamic SQL, in the Oracle database development Pl/sql Block We use the SQL is divided into: Static SQL statements and dynamic SQL statements. The so-called static SQL refers to the SQL statement used in the Pl/sql block is clear at compile time, and the execution is to determine the object. Dynamic SQL means that SQL statements are indeterminate when pl/sql blocks are compiled, such as performing different operations depending on the parameters entered by the user. The compiler does not process the dynamic statement part, simply creates the statement dynamically when the program is run, parsing the statement, and executing the statement.

Dynamic SQL in Oracle can be executed either through local dynamic SQL or through a dbms_sql package. The following two situations are described separately:

One, local dynamic SQL

Local dynamic SQL is implemented using the EXECUTE IMMEDIATE statement.

1. Local Dynamic SQL Execution DDL statement:

Requirements: Based on the user Input table name and field name and other parameters dynamic table.

Create or replace procedure Proc_test

TABLE_NAME in VARCHAR2,--table name

Field1 in Varchar2,--field name

Datatype1 in Varchar2,--field type

Field2 in Varchar2,--field name

Datatype2 in Varchar2--field type

) as

Str_sql VARCHAR2 (500);

Begin

str_sql:= ' CREATE table ' | | table_name| | ' (' | | field1| | ' ' | | datatype1| | ', ' | | field2| | ' ' | | datatype2| | ' )’;

Execute immediate str_sql; --Dynamically executing DDL statements

exception

When others then

Null

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.