- 如何在PLSQL 的預存程序中執行SQL 陳述式呢?
用到動態執行SQL:EXECUTE IMMEDIATE
select * from emp_temp;
CREATE OR REPLACE PROCEDURE F_DELETE_ROWS(table_name IN VARCHAR2,condition IN VARCHAR2 DEFAULT NULL) AS where_clause VARCHAR2(100) := ' WHERE ' || condition; v_table VARCHAR2(30); V_SQL VARCHAR2(100); /**NAME : F_CALCULATE_DATAITEM *PURPOSE :動態刪除表中資料 *IMPUT : table_name 即將刪除資料所屬的表 condition 刪除條件 *Author : -- CICI *CreateDate : -- 2012、12、30 *UpdateDate : -- ************************************************************/BEGIN -- first make sure that the table actually exists; if not, raise an exception SELECT o.OBJECT_NAME INTO v_table FROM SYS.all_objects o WHERE o.OBJECT_NAME = UPPER(table_name) AND o.OBJECT_TYPE = 'TABLE'; IF condition IS NULL THEN where_clause := NULL; END IF; IF table_name IS NOT NULL THEN v_table := table_name; END IF; --拼字即將執行的動態SQL V_SQL := 'DELETE FROM ' || v_table || where_clause; --執行刪除語句 EXECUTE IMMEDIATE V_SQL;EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Invalid table: ' || table_name);END;