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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Recursive Query

Author  Topic 

rnatarajan1
Starting Member

4 Posts

Posted - 2009-08-14 : 11:34:53
Hi,

I have an employeetable which has Userid,name,role,manageruserid and flag. Reporting Manager's userid is stored for each record. ManagerUserid decides the organisation Hiererchy

For a team, I have one manager (Top Manager) whose flag is set to Yes.
Userid is Primary Key.
Now For any particular record, I need to get record whose flag is 'Yes'
An employee will have managerid and inturn that managerid is userid

For any record, I need the managers manager whose flag is set to Yes (Ie: search till the flag is set to Yes).
For Ex:
Manager5 (Flag = Yes) <--Manager4 (Flag= No)<--Manager 3 (Flag=No)<-- Employee
For other employee the flag set to NO

Anyone please help me out

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-08-14 : 11:41:46
Are you really using sql server 2000?

If you are on sql server 2005 you can use a recursive common table expression.



Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

rnatarajan1
Starting Member

4 Posts

Posted - 2009-08-14 : 23:48:11
Im using SQL server 2000
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-08-17 : 05:09:56
Oh well that's a bit of shame -- they are great for this situation.

However, Does this work for you?

-- Data
DECLARE @foo TABLE (
[name] VARCHAR(255)
, [Id] INT
, [managerId] INT
, [status] VARCHAR(3)
)

INSERT @foo ([name], [Id], [managerId], [status])
SELECT 'TheBoss', 1, NULL, 'YES'
UNION SELECT 'MD', 2, 1, 'No'
UNION SELECT 'Line Manager', 3, 2, 'No'
UNION SELECT 'Spod', 4, 3, 'No'
UNION SELECT 'Sub Boss', 5, 1, 'YES'
UNION SELECT 'Spod 2', 6, 5, 'No'

-- Query

DECLARE @map TABLE (
[empId] INT
, [managerId] INT
, [path] VARCHAR(8000)
, [found] BIT
)

DECLARE @rows INT
DECLARE @maxIts INT
DECLARE @its INT


SET @rows = 1
SET @maxIts = 1000
SET @its = 0


INSERT INTO @map ([empId], [managerId], [path], [found])
SELECT
f.[Id]
, CASE f.[status] WHEN 'YES' THEN f.[Id] ELSE f.[managerID] END
, f.[name]
, CASE f.[status] WHEN 'YES' THEN 1 ELSE 0 END
FROM
@foo f

-- Show before
SELECT * FROM @map

WHILE ( @rows > 0 AND @its < @maxIts ) BEGIN

UPDATE m SET
[managerId] = CASE f.[status] WHEN 'YES' THEN f.[Id] ELSE f.[managerId] END
, [path] = [path] + ' -> ' + f.[name]
, [found] = CASE f.[status] WHEN 'YES' THEN 1 ELSE 0 END
FROM
@map m
JOIN @foo f ON f.[ID] = m.[managerID]
WHERE
m.[found] = 0

SET @rows = @@ROWCOUNT
SET @its = @its + 1

END

-- Show map after while loop
SELECT * FROM @map

-- Results
SELECT
f.[name] AS [Employee]
, f2.[name] AS [Manager Who can Say YES]
, m.[path] AS [Management Path]
FROM
@foo f
JOIN @map m ON m.[empId] = f.[ID]
LEFT JOIN @foo f2 ON f2.[Id] = m.[managerId]



Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -