Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Employee - ID, Name, ManagerID, ManagerName

ID     Name    ManagerID     ManagerName
1      Abhi       1        
2      Ram        1                      
3      Sonu       2                  
4      Monu       3                      
5      Shyam      2                    
6      Keshri     4                    
7      Partha     5                               
8      Ramesh     3                           
9      Rajib      7



How to update last column ManagerName by ID Name using store procedure?

[edit]Code block added - OriginalGriff[/edit]

What I have tried:

How to update last column by EmployeeID Name using store procedure
Posted
Updated 19-Sep-17 17:27pm
v2

You don't need to, or want to - you'd be duplicating info, and it would be liable to error should a manager leave and be replaced for example.
Instead, use a JOIN when you SELECT the rows:
SQL
SELECT a.ID, a.Name, a.ManagerID, b.Name AS ManagerName FROM Managers a
JOIN Managers b ON a.ManagerID = b.ID
 
Share this answer
 
v2
Comments
Abhishek Burrnwal 19-Sep-17 12:39pm    
My question is how to update ManagerName column not a select query?
OriginalGriff 19-Sep-17 14:00pm    
And I'm telling you that it's the wrong thing to do: it wastes space and is prone to inaccuracy. Storing data in multiple places is a mistake.

Using a select is a much better solution as the result is always correct.
CREATE PROCEDURE dbo.SpUpdateManager
    @id int = 0,
    @managerName int  
AS
begin
update TableName set ManagerName=@managerName where id  = @id
end
 
Share this answer
 
v2

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