I know I need dynamic sql, but haven't gotten this to work:

I want to loop through a table and for each row, return the value of the field that is located by two columns for that row.

Master table:

Tableabc        Columnabc   
Tabledef        Columndef
Tablexyz        Columnxyz

And I'm looking to return basically

Select [column] from [table] where column = 'desiredtext' 

Where [column] and [table] are from the same row of another table, looping through all rows of the master table.

So, I'm basically searching hundreds of tables in my database for one specific textphrase, where the column and table names are not always the same, however they are readily identified by the query that results in the master table being created.

The goal being to search specific columns in the couple hundred tables that might have the 'desiredtext' I'm looking for, rather than search the entire database of thousands of tables.

What other info can I provide to help someone help me?



  • It might help to provide the field list for the master table and data types. There is just something missing from the question. – CoveGeek May 24 '16 at 2:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.