Click here to Skip to main content
15,889,403 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
advisor_id  advisor_name introducer_name intro_advisor_id
1            A              A
2            B              A
3            C              B
4            D              C
5            E              C
6            F              E


both name as same
they are introduce to each other

how to update the column intro_advisor_id

advisor_id  advisor_name introducer_name  intro_advisor_id
1            A              A                1
2            B              A                1
3            C              B                2
4            D              C                3
5            E              C                3
6            F              E                5

Like this i have a bulk records and i used this query


SQL
declare @advisor_id as int
declare @advisor_name as nvarchar(max)
declare @advisor_code as nvarchar(max)
set @advisor_id=1
select @advisor_id=advisor_id,@advisor_name=advisor_name from tbl_advisor_dummy where advisor_id=@advisor_id
while(@advisor_id<501)
begin
update tbl_advisor_dummy  set create_by_user_id=@advisor_id where introducer_name = @advisor_name
set @advisor_id=@advisor_id+1
end

but its not working, I am stuck with this

thanks,
Posted
Updated 17-Dec-13 2:17am
v4
Comments
pravin2007.cbe 17-Dec-13 8:37am    
Thr is no create_by_user_id field in your table(Used in Updation)
King Fisher 17-Dec-13 23:15pm    
yes that is intro_advisor_id,i mentioned here wrongly
Mike Meinz 17-Dec-13 9:04am    
Many people have the same name!
What happens if two or more advisors or two or more introducers have the same name?
It is not a best practice to use a person's name as a key in a Select/Update operation.
King Fisher 17-Dec-13 23:21pm    
im sorry ,actually i am just migrating the data based on records what i got from the clients

1 solution

You want a JOIN:
SQL
UPDATE A SET A.intro_advisor_id=U.advisor_id
FROM tbl_advisor_dummy A
JOIN tbl_advisor_dummy U
ON U.advisor_name = A.introducer_name
 
Share this answer
 
Comments
Maciej Los 17-Dec-13 8:54am    
5ed!
King Fisher 17-Dec-13 23:22pm    
this is awesome,thanks a lot
OriginalGriff 18-Dec-13 1:49am    
You're welcome!
King Fisher 18-Dec-13 3:54am    
can u guess, whats a mistake in my query?
OriginalGriff 18-Dec-13 3:59am    
Um. You wanted to type it in Orange?
No, go on, give me a clue! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900