The question may not exactly describe what I’d like to do, but I have a stored procedure that has a query returning the following columns
ID Title Tablename
The Title is currently being returned as NULL as I need to be able to get it from a JOINED table referenced in the Tablename column. There could be several different Tablename values returned in the query and the Title for each record needs to be got from the Table referenced in the Tablename field for each record.
Is there a pure SQL way of doing this?
I’ve thought about creating a temporary table and then looping through each record and setting the Title by running a seperate query against the table in Tablename, but am hopign for a cleaner solution.
Thanks
This may be an indication of a design not directly suited to SQL. What is the reason that your Title information is stored in multiple table? Can it be re-factored to not be so?
If, however, the list of tables is a fixed list, there is a solution; use
UNION ALLin a view, or CTE to use an in-line view, to make your many tables look like one table…If you can’t make it fit that, or the number of tables you need to include in the view is not fixed, you will probably need a loop and some dynamic SQL. Again, a strong indicator that the schema isn’t really SQL friendly.