?
Solved

how to update a table recursively sql server 2005?

Posted on 2009-07-07
4
Medium Priority
?
452 Views
Last Modified: 2012-05-07
Hello Experts,


There is an Employee table with (Empid, empname, ManagerID, salary)

i need toi update salary for all employees of s perticular manager? I am trying with recursive CTE syntax. But no luck.

Could you please help me on this?

Thanks,
itsvtk



0
Comment
Question by:Thandava Vallepalli
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24799907
why cant you get the ids of all the employee reporting to the manager

;WITH CTE_EmpList ( ManagerID )
AS (
-- anchor members
SELECT e.ManagerID
FROM Employee e
WHERE ManagerID = @managerID

UNION ALL
-- recursive members
SELECT e.ManagerID
FROM Employee e
INNER JOIN CTE_EmpList d ON e.ManagerID = d.EmployeeID
)
0
 
LVL 15

Expert Comment

by:rob_farley
ID: 24799996
Do you need recursion? What about:

UPDATE Employee SET ...
WHERE ManagerID = @MgrID
0
 
LVL 15

Accepted Solution

by:
rob_farley earned 750 total points
ID: 24800012
If you need the whole Organisation Structure under that manager, then try:

--Make sure the previous query has ended in a semi-colon.

WITH EmployeesToUpdate AS (
--Start with the Manager themself
SELECT @MgrID AS EmployeeID
UNION ALL
--And now pull in the people that report to them (recursively)
SELECT e.EmployeeID
FROM Employee e JOIN EmployeesToUpdate m
  ON e.ManagerID = m.EmployeeID
)
UPDATE e SET ....
FROM Employee e JOIN EmployeesToUpdate u
  ON e.EmployeeID = u.EmployeeID
;
 
0
 
LVL 41

Expert Comment

by:Sharath
ID: 24800201
Is it possible to explain with some sample data and how do you want to update?
0

Featured Post

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question