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
 Development Tools
 Other Development Tools
 UPDATE Table 1 from Table 2 with only new value

Author  Topic 

iboumiza
Starting Member

9 Posts

Posted - 2011-08-15 : 17:51:58
Hi,

then here is my conditions:

1- Table 1 is old and need update and contains clients name

2- Table 2 is new and contain table 1 clients name and new names also

3- Table 2 will update Table 1 with new clients name only.


How do I design the query, please ?

Thanks

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2011-08-15 : 23:53:32
INSERT t1 (column list)
SELECT t2.col1, t2.col2....
FROM t1
LEFT JOIN t2
On t1.pk = t2.pk
WHERE t1.pk IS NULL;
Go to Top of Page

iboumiza
Starting Member

9 Posts

Posted - 2011-08-16 : 00:39:38
Hi,

the query is not working! it shows me 0 rows to append...
PS: the clients name can be the same on both tables but they have different Primary Keys! Maybe the condition should be set differently?
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2011-08-16 : 05:31:29
INSERT t1 (column list)
SELECT t2.col1, t2.col2....
FROM t1
LEFT JOIN t2
On t2.name = t1.name
WHERE t2.name IS NULL;
Go to Top of Page

iboumiza
Starting Member

9 Posts

Posted - 2011-08-16 : 09:17:44
Hi,
I designed like this and now it's working:

Table 2 will Append Table 1 with new clients name only.

 INSERT INTO Table1 ( Table1Name )
SELECT Table2.Table2Name
FROM Table2 LEFT JOIN Table1 ON Table2.Table2Name = Table1.Table1Name
WHERE (((Table1.Table1Name) Is Null));


Thank you for your help!
Go to Top of Page
   

- Advertisement -