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)
 Need Help with this Query!

Author  Topic 

Brittney10
Posting Yak Master

154 Posts

Posted - 2012-01-19 : 09:23:58
I have 4 fields: Primary_Id, User_Id, Admin_Id, and Admin_Name. I need to be able to update the Admin_ID with the Primary_ID if the User_ID and Mgr_Name match (i.e find the Admin_ID for a particular User_ID). For example:

Primary_ID............User_ID..............Admin_Id........Admin_Name
1....................John.Doe..............NULL............Jane.Doe
2....................Jane.Doe..............NULL............Joe.Doe
3....................James.Doe.............NULL............John.Doe
4....................Joe.Doe...............NULL............Jane.Doe



This would become:

Primary_ID............User_ID..............Admin_Id.........Admin_Name
1....................John.Doe...............2...............Jane.Doe
2....................Jane.Doe...............4...............Joe.Doe
3....................James.Doe..............1.............. John.Doe
4....................Joe.Doe................2...............Jane.Doe

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-01-19 : 09:27:37
Test it:

update t1
set Admin_Id = t2.Primary_Id
from YourTable t1
join YourTable t2 on t1.Admin_Name = t2.User_Id



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Brittney10
Posting Yak Master

154 Posts

Posted - 2012-01-19 : 09:59:07
quote:
Originally posted by webfred

Test it:

update t1
set Admin_Id = t2.Primary_Id
from YourTable t1
join YourTable t2 on t1.Admin_Name = t2.User_Id



No, you're never too old to Yak'n'Roll if you're too young to die.



Yes that work perfectly! Thanks!
Go to Top of Page
   

- Advertisement -