Problem solve Get help with specific problems with your technologies, process and projects.

Create procedures and compile DDL scripts on the fly

This Oracle procedure will create views, procedures, functions, packages, update, delete, insert statements and compile them.

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 9.1.0.1, 9.2.0.2.1 and 9.2.0.4.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; 
/ 

Reader Feedback

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.

This was last published in January 2004

Dig Deeper on Oracle database design and architecture

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide.com

SearchDataCenter

SearchContentManagement

SearchHRSoftware

Close