I have a table TABLES_IDS that looks like this:
Table_ID Table_Name Table_Column
-------- ---------- ------------
100 Attributes Attribute_id
101 NewSamples Section_id
...
I have a “first query” that returns several values of Table_Name. This Table_Name is actually the name of another table, and Table_Column is a column. I need to do the following:
- For every obtained
Table_Name, I need to retrieve correspondingTable_Columnvalue. -
Using
Table_NameandTable_Column, create a new sql query that looks e.g. asSELECT FROM Table_Name WHERE Table_Column = 12345 -
Automatically repeat everyting for every
Table_Namereturned by the very first query.
If I understand your question, you want to run a series of queries based on the table / column name stored in this table. You can’t reference table and column names as variables or coming from the result of a query or expression, so you need to use dynamic SQL, e.g.