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.
| Author |
Topic |
|
sql777
Constraint Violating Yak Guru
314 Posts |
Posted - 2005-03-21 : 09:18:24
|
| I want a query that will populate the ReferralUserID in the table below. ReferralUserID can be populated by referencing the ReferralUserName and doing a lookup for the UserID.So ReferralUserID is the UserID of the username under ReferralUserName.TableUsersUserID UserName ReferralUserName ReferralUserID1 John Jerry NULL2 Mark Mommy NULL3 Jerry John NULL4 Donny Mark NULL5 Mommy John NULLExpected Output for ReferralUserID:3 (Jerry)5 (Mommy)1 (John)2 (Mark)1 (John) |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-03-21 : 09:57:55
|
[code]declare @TableUsers table (UserID int, UserName varchar(50), ReferralUserName varchar(50), ReferralUserID int)insert into @TableUsersselect 1, 'John', 'Jerry', NULL union allselect 2, 'Mark', 'Mommy', NULL union allselect 3, 'Jerry', 'John', NULL union allselect 4, 'Donny', 'Mark', NULL union allselect 5, 'Mommy', 'John', NULLupdate t1 set ReferralUserID = t2.useridfrom @TableUsers t1 left join @TableUsers t2 on t2.UserName = t1.ReferralUserNameselect * from @TableUsers[/code]Go with the flow & have fun! Else fight the flow |
 |
|
|
sql777
Constraint Violating Yak Guru
314 Posts |
Posted - 2005-03-21 : 12:22:47
|
| thanks! |
 |
|
|
|
|
|
|
|