How to pass a table name as a parameter to a stored that runs an update statement on that table in oracle
passing table name as parameter in oracle stored procedure
10.2k Views Asked by Karthikaeyen GV At
2
There are 2 best solutions below
1
On
Do not allow users to enter random table names or you will find that they are changing data you did not expect - at the very least you need to whitelist which tables you are expecting them to pass in:
PROCEDURE updateTables(
table_name VARCHAR2,
value1 VARCHAR2,
value2 VARHCAR2,
value3 VARCHAR2
)
IS
BEGIN
IF table_name = 'table1' THEN
UPDATE table1
SET col1 = value1
WHERE col2 = value2;
ELSIF table_name = 'table2' THEN
UPDATE table2
SET col1 = value1,
col3 = value2
WHERE col2 = value3;
ELSIF table_name = 'table3' THEN
UPDATE table3
SET col1 = value3,
col2 = value1,
col3 = value2
WHERE col1 = value3;
ELSE
RAISE_APPLICATION_ERROR( -20001, 'Invalid Table Name' );
END IF;
END;
/
You will need to use Dynamic sql for this something like....