This procedure will create views, procedures, functions and packages, as well as update, delete and insert statements and compile them. This provides developers with a means to call a procedure to create and compile all DDL scripts on the fly and see the output errors when executing stored DDL. The script also controls developer access to the individual schemas. It has been tested on Oracle 18.104.22.168, 22.214.171.124.1 and 126.96.36.199.0.
CREATE OR REPLACE PROCEDURE execute_immediate(p_sql_text VARCHAR2) IS COMPILATION_ERROR EXCEPTION; PRAGMA EXCEPTION_INIT(COMPILATION_ERROR,-20009); l_cursor INTEGER DEFAULT 0; rc INTEGER DEFAULT 0; BEGIN l_cursor := DBMS_SQL.OPEN_CURSOR; DBMS_SQL.PARSE(l_cursor, p_sql_text, DBMS_SQL.NATIVE); rc := DBMS_SQL.EXECUTE(l_cursor); DBMS_SQL.CLOSE_CURSOR(l_cursor); -- -- Ignore compilation errors because these sometimes happen due to -- dependencies between views AND procedures -- EXCEPTION WHEN COMPILATION_ERROR THEN DBMS_SQL.CLOSE_CURSOR(l_cursor); WHEN OTHERS THEN BEGIN DBMS_SQL.CLOSE_CURSOR(l_cursor); RAISE_APPLICATION_ERROR(-20010,sqlerrm || ' when executing ''' || p_sql_text || ''' '); END; END; /
Dan C. writes: Extremely dangerous -- nothing in it to prevent a user from dropping schema objects or the schema itself. Also somewhat redundant, as a developer can put simple Execute Immediate statements anywhere in their code, with better security. It completely hides any compilation error, so you don't even know whether it did anything. A routine such as this should at least offer some high-value service, such as logging the request and who issued it, and whether it succeeded or failed. I would never put such a routine into a production environment.
For More Information
- Feedback: E-mail the editor with your thoughts about this tip.
- More tips: Hundreds of free Oracle tips and scripts.
- Tip contest: Have an Oracle tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize -- submit your tip today!
- Ask the Experts: Our applications, SQL, database administration, and data warehousing gurus are waiting to answer your toughest questions.
- Forums: Ask your technical Oracle questions--or help out your peers by answering them--in our active forums.
- Best Web Links: Oracle tips, tutorials, and scripts from around the Web.