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 2008 Forums
 Transact-SQL (2008)
 building datatable from more than 1 database

Author  Topic 

evvo
Starting Member

16 Posts

Posted - 2011-09-27 : 10:55:17

Im seeking to create a datatable from 2 or 3 different database tables.

Table A provides an ID and Name, so i simply grab all these instances.

i then need to provide a value field for each of these entries from 1 or 2 databases depending on whether the value exists for that ID.

So the order is - if Table B has a value for the ID AND users countryID i use that ELSE the value for ID AND countryID of NULL

if neither of these exist i then need to check Table C for either a entry for the ID AND users country or the ID and UK entry which always exists, so the final datatable will have a value for each ID that existed in Table A.

how would i construct this sql in my stored procedure?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-27 : 11:37:16
sounds something like below


SELECT a.ID,b.columns.....,c.columns....
FROM TableA a
LEFT JOIN TableB b
ON b.ID = a.ID
LEFT JOIN TableC c
ON c.ID = a.ID


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -