0

I have table Employee. I need to update manager id using excel sheet (to get value from excel I have written below query)

Employee

Emp_ID  Emp_Code Name  ManagerID
1       1111     xyz    2
2       2222     abc    3
3       3333     mno    2

I have written query like this

UPDATE Employee  SET ManagerID = Emp_ID   from Employee  inner join  Employee AS MGR 
on Employee.emp_id=MGR.emp_id    WHERE  emp_code='1111'

but its not updating the correct value

1
  • Have you stored the values of excel in a table? If so what is the structure of the table? Commented Aug 2, 2012 at 6:24

1 Answer 1

1

Based on what you've told us, you don't need a self join at all, i.e. you can simply update an employee's manager id directly

UPDATE Employee  
    SET ManagerID = 2 
    WHERE  emp_code='1111'

etc

However, if you mean that you need to perform the update given only the emp_code of the employee and the emp_code of the manager (i.e. without the Manager's PK), then you can use a subquery (uncorrelated), e.g.

UPDATE Employee  
    SET ManagerID = (SELECT manager.Emp_Id 
                       FROM Employee manager 
                       WHERE manager.emp_code = '2222') -- Manager's emp_code
    WHERE  emp_code='1111' -- Employee to update's emp_code

If you then later need to add a query showing the employee and his / her manager (and assuming that the ultimate boss doesn't have a manager), you can do the self join like so:

SELECT emp.emp_code as EmployeeCode, emp.name as EmployeeName, 
       mgr.emp_code as ManagerEmpCode, mgr.name as ManagerName
FROM Employee emp 
   LEFT OUTER JOIN Employee mgr 
   ON emp.ManagerId = mgr.Emp_Id
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.