How can I pass a column name as a parameter to a SQL statement ?
I have a table with columns named Log1,Log2,...,Log10.
I want to run a loop and for each cicle to update the alue in LogX column.
This is my code :
<<
DECLARE
logX myTable.Log1%TYPE;
BEGIN
for i IN 1..11 LOOP
logX := CONCAT('Log',TO_CHAR(i,'99'));
UPDATE myTable set &logX='010100000000000';
end loop;
END;
>>
"logX" is the varaible that contais the current column name.
Thanks,
Richard
You can build a sql statement in a character string
and then use the execute immediate command to run it.
Here's an example. I'm using a bind variable to hold
the column I want to update. It could also be passed
as a parameter to a stored procedure. The approach would
be basically the same.
Make a sample table and load it.
sys@AMSAA> create table testit
as select * from tab;
You would typically not want to use :myval the way I have.
You'd want to use it as a bind variable in the execute immediate command so that your SQL is reusable. I left that out for clarity. I'm also going to update just the first row in this example.
Bookmarks