Search
 
SCRIPT & CODE EXAMPLE
 

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.
*/
Comment

PREVIOUS NEXT
Code Example
Sql :: error code 1292 mysql workbench 
Sql :: postgresql show current database 
Sql :: mysql on terminal mac 
Sql :: oracle kill job by sid 
Sql :: oracle get trigger ddl 
Sql :: get the mysql table columns data type mysql 
Sql :: sql syntax find few columns 
Sql :: scaffold npgsql net core 
Sql :: laravel migration sql dump 
Sql :: psql human readable 
Sql :: postgresql get last day of month 
Sql :: my sql version 
Sql :: rows to comma separated values in mssql 
Sql :: What is the compatibility level of a SQL database 
Sql :: mysql case when null 
Sql :: dbms_output.put_line 
Sql :: default password of mysql 
Sql :: oracle current date plus 1 month 
Sql :: SQL server query column yes or no 
Sql :: What is localhost IP and MySql default port no. 
Sql :: get last record in sql 
Sql :: oracle create program if no exists 
Sql :: postgres describe query 
Sql :: hour and minute between two datatime sql 
Sql :: grant mysql 
Sql :: mysql output csv 
Sql :: oracle add proxy 
Sql :: An error occurred while installing mysql2 (0.3.20), and Bundler cannot continue. 
Sql :: check postgresql port windows 
Sql :: insert all in sql 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =