Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Merge Tables in SQL 2005

By rka,

1. First Create the Database

2. Then create the tables

3. Insert the data

4. I would like to merge Table1, Table2 and Table3 based on matching ID's.

Table Data is:

Table1

Table2

Table3

Destination

 

5. The logic and the expected result is:

IF ID is found THEN

return field1 OR field2 OR field3 from Table1 OR Table2 OR Table 3

ELSE

return values from DESTINATION table

END IF

 

id field1 field2 field3
1 a b c
2 d e f
3 g h i
4 j k l

 

6. The actual result we get after executing the query:

Total article views: 1327 | Views in the last 30 days: 1
 
Related Articles
FORUM

Copying Table1 to Table2

hi guys, can you give me some ideas how to do this? i have table1 and table2. i want to copy the ...

FORUM

Table Performance issue on select query

Select * from Table1 and Select * from Table2

FORUM

UPDATE table1 with table2 data

Just wondering if this is the best way to do this update statement: [code] UPDATE [table1] SET ...

FORUM

Difference between table1, table2 and table1 inner join table2

Hi, What's the difference between using Table1, Table2 in FROM or using a join between both tables?...

FORUM

select table1 data based on table2 fields

I have table1 like so: ID    Mo    Year    In1    In2 A      1     2004    1        1 A      2     ...

 
Contribute