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 2005 Forums
 Transact-SQL (2005)
 Update using two tables

Author  Topic 

fralo
Posting Yak Master

161 Posts

Posted - 2010-03-10 : 11:37:33
I need this ASAP. I need to update 'windowsusername' field in table A with what is in the 'username' field in Table B. The link between the two tables is email field.

Thanks for your help.

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2010-03-10 : 11:41:55
update a
set windowusername = b.username
from TableA a
inner join
TableB b
on a.email = b.email

=======================================
There are no passengers on spaceship earth. We are all crew. -Marshall McLuhan, educator and philosopher (1911-1980)
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-03-10 : 11:44:23
"I need this ASAP"

Then pay a consultant instead of relying on people's free time here!

UPDATE A
SET A.windowsusername = B.username
FROM TableA AS A
JOIN TableB AS B
ON B.email = A.email
WHERE COALESCE(A.windowsusername, '') <> COALESCE(B.username, '')
Go to Top of Page

fralo
Posting Yak Master

161 Posts

Posted - 2010-03-10 : 11:53:14
Sorry! Didn't mean to sound harsh. I should have worded it differently.

Thanks for your help.
Go to Top of Page

fralo
Posting Yak Master

161 Posts

Posted - 2010-03-10 : 12:32:41
When I use this it updates everything to the same value!

update a
set windowusername = b.username
from TableA a
inner join
TableB b
on a.email = b.email

I thought this would fix it.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-10 : 12:42:38
quote:
Originally posted by fralo

When I use this it updates everything to the same value!

update a
set windowusername = b.username
from TableA a
inner join
TableB b
on a.email = b.email

I thought this would fix it.


then it may be that you've same value for all rows of table b

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

Go to Top of Page

fralo
Posting Yak Master

161 Posts

Posted - 2010-03-10 : 13:22:26
When I execute it, I receive an error:

"The multi-part identifier 'USERS_1.WindowsUserName could not be found."

It is changing the name of my table from USERS to USERS_1 for some strange reason.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-03-10 : 13:45:56
Suggest you post the EXACT query you are using pls.
Go to Top of Page
   

- Advertisement -