Oracle PL/SQL Tutorial/PL SQL Statements/Dynamic SQL statement
Содержание
An example of code using native dynamic SQL
SQL> DECLARE
2 tableName VARCHAR2(30):="temp";
3 sqlString VARCHAR2(150);
4 BEGIN
5 sqlString :="create table "||tableName||"(col1 NUMBER NOT NULL)";
6 EXECUTE IMMEDIATE sqlString;
7 END;
8 /
PL/SQL procedure successfully completed.
SQL>
SQL>
SQL> drop table temp;
Table dropped.
Create dynamic table
SQL> Create or replace Procedure create_dyn_table2
2 (i_region_name VARCHAR2,
3 returnCode OUT NUMBER,
4 errorMessage OUT VARCHAR2)
5 authid current_user
6 Is
7 sqlString VARCHAR2(1000);
8 tableName VARCHAR2(30);
9 Begin
10 tableName :="ORDER_ITEMS_FOR_"||replace(trim(i_region_name),"","_");
11 sqlString :="CREATE TABLE "||tableName||
12 "(order_id NUMBER(10)NOT NULL,
13 item_id VARCHAR2(10)NOT NULL,
14 unit_price NUMBER(11,2)NOT NULL,
15 quantity NUMBER)";
16
17 EXECUTE IMMEDIATE sqlString ;
18
19 returnCode :=0;
20
21 EXCEPTION WHEN OTHERS THEN
22
23 returnCode :=SQLCODE;
24
25 errorMessage :="ERR:Creating/Altering table "||tableName ||"-"||SQLERRM;
26 End;
27 /
Procedure created.
SQL>
SQL>
SQL>
Dynamic DDL
SQL> Create or replace Procedure create_dyn_table
2 (i_region_name VARCHAR2,
3 returnCode OUT NUMBER,
4 errorMessage OUT VARCHAR2)
5 authid current_user
6 Is
7 sqlString VARCHAR2(1000);
8 tableName VARCHAR2(30);
9 Begin
10 tableName :="ORDERS_FOR_"||replace(trim(i_region_name),"","_");
11 sqlString :="CREATE TABLE "||tableName||
12 "(order_id NUMBER(10)PRIMARY KEY,
13 sale_date DATE NOT NULL,
14 total_number NUMBER,
15 total_price NUMBER(15,2),
16 company_id NUMBER(6))";
17 EXECUTE IMMEDIATE sqlString ;
18 returnCode :=0;
19 EXCEPTION WHEN OTHERS THEN
20 returnCode :=SQLCODE;
21 errorMessage :="ERR:Creating table "||tableName ||"-"||SQLERRM;
22 End;
23 /
Procedure created.
SQL>
SQL>
SQL>
Dynamic DQL with DBMS_SQL package
SQL>
SQL> CREATE OR REPLACE PROCEDURE runddl (ddl_in in VARCHAR2)
2 IS
3 cur INTEGER:= DBMS_SQL.OPEN_CURSOR;
4 returnValue INTEGER;
5 BEGIN
6 DBMS_SQL.PARSE (cur, ddl_in, DBMS_SQL.NATIVE);
7 returnValue := DBMS_SQL.EXECUTE (cur);
8 DBMS_SQL.CLOSE_CURSOR (cur);
9 EXCEPTION
10 WHEN OTHERS
11 THEN
12 DBMS_OUTPUT.PUT_LINE (
13 "RunDDL Failure on " || ddl_in);
14 DBMS_OUTPUT.PUT_LINE (SQLERRM);
15 DBMS_SQL.CLOSE_CURSOR (cur);
16 END;
17 /
Procedure created.
SQL>
SQL>
SQL>
Dynamic PL/SQL
SQL>
SQL>
SQL> CREATE TABLE customer_region(
2 region_id NUMBER(4) PRIMARY KEY,
3 region_name VARCHAR2(11) NOT NULL
4 );
Table created.
SQL>
SQL>
SQL> INSERT INTO customer_region VALUES (1,"REGION1");
1 row created.
SQL> INSERT INTO customer_region VALUES (2,"REGION2");
1 row created.
SQL> INSERT INTO customer_region VALUES (3,"REGION3");
1 row created.
SQL> INSERT INTO customer_region VALUES (4,"REGION4");
1 row created.
SQL>
SQL>
SQL> Create or replace Procedure myProc(returnCode OUT NUMBER,errorMessage OUT VARCHAR2)
2 Is
3 CURSOR csr_region IS SELECT region_name FROM customer_region;
4 Dyn_proc_name VARCHAR2(100);
5 Dyn_plsql_string VARCHAR2(1000);
6 BEGIN
7 FOR idx IN csr_region LOOP
8 EXECUTE IMMEDIATE "BEGIN update_dyn_"||replace(trim(idx.region_name),"","_")||"(:1,:2);END;"
9 USING OUT returnCode,OUT errorMessage;
10 IF returnCode <>0 THEN
11 EXIT;
12 END IF;
13 END LOOP;
14 EXCEPTION WHEN OTHERS THEN
15 returnCode := SQLCODE;
16 errorMessage := SQLERRM;
17 END;
18 /
Procedure created.
SQL>
SQL> drop table customer_region;
Table dropped.
SQL>
SQL>
SQL>
Table row counter
SQL>
SQL> CREATE TABLE EMP (EMPNO NUMBER(4) NOT NULL,
2 ENAME VARCHAR2(10),
3 JOB VARCHAR2(9),
4 MGR NUMBER(4),
5 HIREDATE DATE,
6 SAL NUMBER(7, 2),
7 COMM NUMBER(7, 2),
8 DEPTNO NUMBER(2));
Table created.
SQL>
SQL> INSERT INTO EMP VALUES (7369, "SMITH", "CLERK", 7902, TO_DATE("17-DEC-1980", "DD-MON-YYYY"), 800, NULL, 20);
1 row created.
SQL> INSERT INTO EMP VALUES (7499, "ALLEN", "SALESMAN", 7698, TO_DATE("20-FEB-1981", "DD-MON-YYYY"), 1600, 300, 30);
1 row created.
SQL> INSERT INTO EMP VALUES (7521, "WARD", "SALESMAN", 7698, TO_DATE("22-FEB-1981", "DD-MON-YYYY"), 1250, 500, 30);
1 row created.
SQL> INSERT INTO EMP VALUES (7566, "JONES", "MANAGER", 7839, TO_DATE("2-APR-1981", "DD-MON-YYYY"), 2975, NULL, 20);
1 row created.
SQL> INSERT INTO EMP VALUES (7654, "MARTIN", "SALESMAN", 7698,TO_DATE("28-SEP-1981", "DD-MON-YYYY"), 1250, 1400, 30);
1 row created.
SQL> INSERT INTO EMP VALUES (7698, "BLAKE", "MANAGER", 7839,TO_DATE("1-MAY-1981", "DD-MON-YYYY"), 2850, NULL, 30);
1 row created.
SQL> INSERT INTO EMP VALUES (7782, "CLARK", "MANAGER", 7839,TO_DATE("9-JUN-1981", "DD-MON-YYYY"), 2450, NULL, 10);
1 row created.
SQL> INSERT INTO EMP VALUES (7788, "SCOTT", "ANALYST", 7566,TO_DATE("09-DEC-1982", "DD-MON-YYYY"), 3000, NULL, 20);
1 row created.
SQL> INSERT INTO EMP VALUES (7839, "KING", "PRESIDENT", NULL,TO_DATE("17-NOV-1981", "DD-MON-YYYY"), 5000, NULL, 10);
1 row created.
SQL> INSERT INTO EMP VALUES (7844, "TURNER", "SALESMAN", 7698,TO_DATE("8-SEP-1981", "DD-MON-YYYY"), 1500, 0, 30);
1 row created.
SQL> INSERT INTO EMP VALUES (7876, "ADAMS", "CLERK", 7788,TO_DATE("12-JAN-1983", "DD-MON-YYYY"), 1100, NULL, 20);
1 row created.
SQL> INSERT INTO EMP VALUES (7900, "JAMES", "CLERK", 7698,TO_DATE("3-DEC-1981", "DD-MON-YYYY"), 950, NULL, 30);
1 row created.
SQL> INSERT INTO EMP VALUES (7902, "FORD", "ANALYST", 7566,TO_DATE("3-DEC-1981", "DD-MON-YYYY"), 3000, NULL, 20);
1 row created.
SQL> INSERT INTO EMP VALUES (7934, "MILLER", "CLERK", 7782,TO_DATE("23-JAN-1982", "DD-MON-YYYY"), 1300, NULL, 10);
1 row created.
SQL>
SQL>
SQL>
SQL> CREATE OR REPLACE FUNCTION tabcount (
2 nm IN VARCHAR2,
3 whr IN VARCHAR2 := NULL)
4 RETURN PLS_INTEGER
5 IS
6 retval PLS_INTEGER;
7 v_where VARCHAR2(2000) := whr;
8 BEGIN
9 IF UPPER (whr) NOT LIKE "WHERE %"
10 THEN
11 v_where := "WHERE " || v_where;
12 END IF;
13
14 EXECUTE IMMEDIATE
15 "SELECT COUNT(*) FROM " || nm || " " || v_where
16 INTO retval;
17 RETURN retval;
18 END;
19 /
Function created.
SQL>
SQL> select tabcount("emp", null) from dual;
TABCOUNT("EMP",NULL)
--------------------
14
SQL>
SQL> drop table emp;
Table dropped.