Skip to content
Advertisement

Using declared variables in SQL Oracle query FOR LOOP

I’m attempting to write a query script where I declare a variable and then use them in a select statement in Oracle SQL.
Basically script should check if parent tables’ entries are used in referenced table or not. if not –> delete the entry from REF table. So far I have this

So far LOG_MSG('Parent table: ' || tableName.table_name || ' , of Reference table: ' || tableName.ref_table_name ); I got a msg log that lists me the variables but I have no idea how to combine that in query DELETE above.

Any ideas or suggestions how it could be automized to this ?

Advertisement

Answer

You can’t reference table names as bind variables. In that case, use EXECUTE IMMEDIATE. I’m a big fan of cursor for loops so I used that. Note that this code is not tested.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement