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
 General SQL Server Forums
 New to SQL Server Programming
 Simple update from another table

Author  Topic 

pvong
Yak Posting Veteran

58 Posts

Posted - 2013-04-17 : 12:08:10
I know this is simple but I can not figure it out.

Table1----------------Table2
ClientID--Date--------ClientID****Date
1---------null--------1***********1/2/2013
2---------null--------3***********4/15/2013
3---------null

Basically, I just want to update table 1 Date from Table 2 but table 2 does not have all clients.

In my example, only client 1 and 3 will have dates while client 2 will stay as Null.

Thanks!

------------------------------
Using VS2010 / Learning in VB.Net / Win2008 R2 / SQL 2008 R2
Be kind to the newbies because you were once there.

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-04-17 : 12:58:02
quote:
Originally posted by pvong

I know this is simple but I can not figure it out.

Table1----------------Table2
ClientID--Date--------ClientID****Date
1---------null--------1***********1/2/2013
2---------null--------3***********4/15/2013
3---------null

Basically, I just want to update table 1 Date from Table 2 but table 2 does not have all clients.

In my example, only client 1 and 3 will have dates while client 2 will stay as Null.

Thanks!

------------------------------
Using VS2010 / Learning in VB.Net / Win2008 R2 / SQL 2008 R2
Be kind to the newbies because you were once there.

What do you want to do for client 2? Leave it as null? Also, will there be more than one row in Table2 for a given ClientID? If there is which of the dates do you want to apply to Table1?

Here is one possible way, which assumes that you want to leave client2 date as null, and that there is only one row in Table2 for a given client id.
update t1 set
[Date] = t2.[Date]
from
Table1 t1
inner join Table2 t2 on
t1.ClientId = t2.ClientId;
Go to Top of Page
   

- Advertisement -