Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SQL

pl sql dynamic sql drop doesnt work

/*Making Procedures Work on Arbitrarily Named Schema Objects

Suppose you need a procedure that accepts the name of any database table, then drops that table from your schema. Using dynamic SQL, you might write the following standalone procedure:
This doesn't work: */
CREATE PROCEDURE drop_table (table_name IN VARCHAR2) AS
BEGIN
   EXECUTE IMMEDIATE 'DROP TABLE :tab' USING table_name;
END;
/*
However, at run time, this procedure fails with an invalid table name error. That is because you cannot use bind arguments to pass the names of schema objects to a dynamic SQL statement. Instead, you must embed parameters in the dynamic string, then pass the names of schema objects to those parameters.

To debug the last example, you must revise the EXECUTE IMMEDIATE statement. Instead of using a placeholder and bind argument, you embed parameter table_name in the dynamic string, as follows:
*/
CREATE PROCEDURE drop_table (table_name IN VARCHAR2) AS
BEGIN
   EXECUTE IMMEDIATE 'DROP TABLE ' || table_name;
END;
/*
Now, you can pass the name of any database table to the dynamic SQL statement.
*/
 
PREVIOUS NEXT
Tagged: #pl #sql #dynamic #sql #drop #doesnt #work
ADD COMMENT
Topic
Name
6+1 =