Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Link 2 tables where 2nd table name is in field from the first table

Status
Not open for further replies.

csutton

Programmer
Joined
Dec 27, 2000
Messages
213
Location
US
Alright... :) let's see what I can do to explain this.

I have a base table that contains the following type of information:

recid (int)
controlnumber (string)
status (int)
tablename (string)
... other non-essential stuff.

Now, table 2 contains other info:

recid (int)
controlnumber (string) <--- this is the link btwn the two
incidentlocation (string)
incidentdate (datetime)
... etc etc

I need to join these 2 tables together to get information into a grid. However, table 2 may change on each record for number 1, meaning sometimes the information from table 1 must be linked to a table 2 called fdddata, sometimes its called fdddata_eht, etc.

Using a static SQL statement, I am doing the following:

select ts_central.status, ts_central.controlnumber,
ts_central.authorid, ts_central.datesubmitted,
fdddata.incdate, fdddata.incloc, fdddata.recid
FROM ts_central, fdddata WHERE ts_central.controlnumber =
fdddata.controlnumber and (status = 3 or status = 5)
and ts_central.authorid = 'csutton'

Again, where fdddata is shown above, sometimes it would be fdddata, sometimes fdddata_eht, etc. depending on the tablename from table 1.

I have no problem using a stored procedure if that would help, but I have no idea where to begin.

THANK YOU!!!!!!!!!!
 
This will need to done via dynamic sql and can only process one second table at a time.

The other option would be to create a temp table, load it up with all the data from the tables you could be joining to with the name of the table that you are pulling from as a column name. Then you can join the controlnumber and tablename columns.

Denny
MCSA (2003) / MCDBA (SQL 2000)
MCTS (SQL 2005 / Microsoft Windows SharePoint Services 3.0: Configuration / Microsoft Office SharePoint Server 2007: Configuration)
MCITP Database Administrator (SQL 2005) / Database Developer (SQL 2005)

--Anything is possible. All it takes is a little research. (Me)
[noevil]
 
Hi Denny,

I've been trying to figure this out and I haven't done dynamic sql before. Can you point me in a direction how this might be done with the above sql statement?

Also, I do have the capability of knowing the other table names that would have the list of tables that would replace fdddata above.

Thanks again.

Chris Sutton
 
What version of SQL Server is this using?

Denny
MCSA (2003) / MCDBA (SQL 2000)
MCTS (SQL 2005 / Microsoft Windows SharePoint Services 3.0: Configuration / Microsoft Office SharePoint Server 2007: Configuration)
MCITP Database Administrator (SQL 2005) / Database Developer (SQL 2005)

--Anything is possible. All it takes is a little research. (Me)
[noevil]
 
2005 will make it easier. Can you post the schemas for the main table as well as 2 child tables please.

Denny
MCSA (2003) / MCDBA (SQL 2000)
MCTS (SQL 2005 / Microsoft Windows SharePoint Services 3.0: Configuration / Microsoft Office SharePoint Server 2007: Configuration)
MCITP Database Administrator (SQL 2005) / Database Developer (SQL 2005)

--Anything is possible. All it takes is a little research. (Me)
[noevil]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top