The
Examples
The following PL/SQL block contains several examples of dynamic SQL:
CREATE OR REPLACE PROCEDURE wysw_hier() AS
BEGIN
execute IMMEDIATE 'CREATE OR REPLACE VIEW dane AS
SELECT sid, snazw
FROM SPOLKA
UNION ALL
SELECT kid, knazw
FROM KOMORKA
UNION ALL
SELECT pid, pnazw
FROM PRACOWNIK';
END;
EXECUTE
IMMEDIATE
statement executes a
dynamic SQL statement or anonymous PL/SQL block. You can use it to issue
SQL statements that cannot be represented directly in PL/SQL, or to
build up statements where you do not know all the table names, WHERE
clauses, and so on in advance. For more informationExamples
The following PL/SQL block contains several examples of dynamic SQL:
DECLARE sql_stmt VARCHAR2(200); plsql_block VARCHAR2(500); emp_id NUMBER(4) := 7566; salary NUMBER(7,2); dept_id NUMBER(2) := 50; dept_name VARCHAR2(14) := 'PERSONNEL'; location VARCHAR2(13) := 'DALLAS'; emp_rec emp%ROWTYPE; BEGIN EXECUTE IMMEDIATE 'CREATE TABLE bonus (id NUMBER, amt NUMBER)'; sql_stmt := 'INSERT INTO dept VALUES (:1, :2, :3)'; EXECUTE IMMEDIATE sql_stmt USING dept_id, dept_name, location; sql_stmt := 'SELECT * FROM emp WHERE empno = :id'; EXECUTE IMMEDIATE sql_stmt INTO emp_rec USING emp_id; plsql_block := 'BEGIN emp_pkg.raise_salary(:id, :amt); END;'; EXECUTE IMMEDIATE plsql_block USING 7788, 500; sql_stmt := 'UPDATE emp SET sal = 2000 WHERE empno = :1 RETURNING sal INTO :2'; EXECUTE IMMEDIATE sql_stmt USING emp_id RETURNING INTO salary; EXECUTE IMMEDIATE 'DELETE FROM dept WHERE deptno = :num' USING dept_id; EXECUTE IMMEDIATE 'ALTER SESSION SET SQL_TRACE TRUE'; END;
CREATE OR REPLACE PROCEDURE wysw_hier() AS
BEGIN
execute IMMEDIATE 'CREATE OR REPLACE VIEW dane AS
SELECT sid, snazw
FROM SPOLKA
UNION ALL
SELECT kid, knazw
FROM KOMORKA
UNION ALL
SELECT pid, pnazw
FROM PRACOWNIK';
END;
No comments:
Post a Comment