Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 how to do Select Query on Table Name from Column

Author  Topic 

hsaryan
Starting Member

1 Post

Posted - 2010-07-30 : 11:40:31
Hi,
I have a table called "Association" that has column "RedordTable" and "RecordID". I am trying to figure out a way to run sub query with in the query that will query the tables from the original table. Here is an Example using Case-When but I would prefer to pull the value from the tables if possible.

[url]www.thaxtonassociates.com/downloads/SQL-Screenshot-1.jpg[/url]
----- Here is an example with Case When and this works fine---
----- NOTE: The Code Below can also be seen in the link above -----
select *,
Case RecordTable
When 'Contacts' then (Select Type from Contacts where Association.RecordID=Contacts.Contactsid)
When 'Dealership' then (Select Type from Dealerships where Association.RecordID=Dealerships.Dealershipsid)
end
from Association
-----------------------------------------------

Ideally I would like the code to look something like. (Or anything similar that you might recommend)
- Please see screen shot below in the link.
[url]www.thaxtonassociates.com/downloads/SQL-Screenshot-2.jpg[/url]

Also please note i have looked into using sp_executeSQL function and I have not been successful either


Thank you in advance for any help.
H.
   

- Advertisement -