Building on Tony’s answer on this question:
If I want to do something like this,
CREATE PROCEDURE A(tab IN VARCHAR2) IS
tab.col_name <column> --static declaration (column name always remains the same)
BEGIN
EXECUTE IMMEDIATE 'INSERT INTO ' || tab(col_name) || 'VALUES(123)';
END A;
How can I use Dynamic SQL in the above case?
This example passes in a table name and a column name:
You need to realise that everything after EXECUTE IMMEDIATE must be a string that contains some valid SQL. A good way to verify this is to set it up in a variable and print it to the screen:
This should then display something like the following in SQL Plus:
(provided server output is turned on).
EDIT: Since you want the column name to be a local variable that always has the same value, this could be done as: