I am trying to write a stored procedure that could create a table called tblManagerHierarchy. It is a table that contains recursive data.
The data is coming from tblEmployee where it contains that EmpNum and ManagerEmpNum. tblEmployee (EmpNum, ManagerEmpNum)
The tblManagerHierarchy should contain Manager's EmpNum plus direct and indirect subordinates' EmpNum.
Basically it should contain everybody who reports to a Manager directly and indirectly. So CEO should contain everyone in the company, and general manager only has few..
The table's structure should be like this. tblManagerHierarchy (ManagerEmpNum, EmpNum)
I need to use store procedure that contains cursor and loop.
Any sample of code is appreciated.
Thanks.
3 4910
I am trying to write a stored procedure that could create a table called tblManagerHierarchy. It is a table that contains recursive data.
The data is coming from tblEmployee where it contains that EmpNum and ManagerEmpNum. tblEmployee (EmpNum, ManagerEmpNum)
The tblManagerHierarchy should contain Manager's EmpNum plus direct and indirect subordinates' EmpNum.
Basically it should contain everybody who reports to a Manager directly and indirectly. So CEO should contain everyone in the company, and general manager only has few..
The table's structure should be like this. tblManagerHierarchy (ManagerEmpNum, EmpNum)
I need to use store procedure that contains cursor and loop.
Any sample of code is appreciated.
Thanks.
That's quite the task. But if you want a recursive function, here you go.. -
CREATE FUNCTION dbo.GetHierarchy(@ManagerID int)
-
RETURNS @Hierarchy table (ParentID int,
-
ChildID int)
-
AS
-
BEGIN
-
-
--EMPNUM, --MANAGEREMPNUM, --TblEmployee
-
-
declare @ChildID int
-
if (@ManagerID int is null)
-
begin
-
declare curEmp cursor local fast_forward for
-
select empNum
-
from tblEmployee
-
-
open curEmp
-
-
fetchnext from curEmp into @ChildID
-
while @@fetch_status = 0
-
begin
-
insert into @Hierarchy(ParentID,ChildID)
-
values(@ManagerID,@ChildID)
-
-
insert into @Hierarchy(ParentID,ChildID)
-
select * from dbo.GetHierarchy(@ChildID)
-
-
fetchnext from curEmp into @ChildID
-
end
-
-
close curEmp
-
deallocate curEmp
-
end
-
else
-
begin
-
declare curEmp cursor local faast_forward for
-
select empNum
-
from tblEmployee
-
where managerempnum = @ManagerID
-
-
open curEmp
-
-
fetchnext from curemp into @ChildID
-
while @@fetch_status = 0
-
begin
-
insert into @Hierarchy(ParentID,ChildID)
-
values (@ManagerID,@ChildID)
-
-
insert into @Hierarchy(parentID,childID)
-
select * from dbo.GetHierarchy(@ChildID)
-
-
fetchnext from curemp into @ChildID
-
-
end
-
-
-
-
end
-
return
-
-
end
-
Or better yet, if you're using sql server 2005 I just found this in the documentation. -
USE AdventureWorks;
-
GO
-
WITH DirectReports(ManagerID, EmployeeID, EmployeeLevel) AS
-
(
-
SELECT ManagerID, EmployeeID, 0 AS EmployeeLevel
-
FROM HumanResources.Employee
-
WHERE ManagerID IS NULL
-
UNION ALL
-
SELECT e.ManagerID, e.EmployeeID, EmployeeLevel + 1
-
FROM HumanResources.Employee e
-
INNER JOIN DirectReports d
-
ON e.ManagerID = d.EmployeeID
-
)
-
SELECT ManagerID, EmployeeID, EmployeeLevel
-
FROM DirectReports ;
-
GO
-
-
-
Very nice
Thank you so much.
I tried your coding, but I got different twist of results than what I expected.
Instead having all the direct and indirect manager's emp num, it listed everyone who is under a manager's chain of command in the relationshiop of Direct Report.
Right Now, it is retreving like this: MgrEmpNum : EmpNum
CEO's : VP's
VP's : AVPs
AVPs : GM's
GMs : TeamLeader
What I am trying to get is like this: MgrEmpNum : EmpNum
CEO's : VP's
CEO's : AVPs
CEO's : GM's
CEO's : TeamLeader
.......
VP's : AVPs
VP's : GM's
VP's : TeamLeader
CREATE FUNCTION dbo.fnPMMgrHierarchy(@ManagerID char(11))
RETURNS @Hierarchy table (ParentID char(11), ChildID char(11))
AS
BEGIN
--EMPNUM, --MANAGEREMPNUM, --TblEmployee
declare @ChildID char(11)
If (@ManagerID is null)
begin
declare @curEmp CURSOR
set @curEmp = CURSOR LOCAL FAST_FORWARD FOR
select sEmpNum
from tblPMEmployee
open @curEmp
fetch next from @curEmp into @ChildID
while @@fetch_status = 0
begin
insert into @Hierarchy(ParentID,ChildID)
values(@ManagerID,@ChildID)
insert into @Hierarchy(ParentID,ChildID)
select * from dbo.fnPMMgrHierarchy(@ChildID)
fetch next from @curEmp into @ChildID
end
close @curEmp
deallocate @curEmp
end
else
begin
set @curEmp = CURSOR LOCAL FAST_FORWARD FOR
select sEmpNum
from tblPMEmployee
where sTmShtSupvEmpNum = @ManagerID
open @curEmp
fetch next from @curEmp into @ChildID
while @@fetch_status = 0
begin
insert into @Hierarchy(ParentID,ChildID)
values (@ManagerID,@ChildID)
insert into @Hierarchy(parentID,childID)
select * from dbo.fnPMMgrHierarchy(@ChildID)
fetch next from @curEmp into @ChildID
end
end
return
end
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Tero Saarni |
last post by:
Hi,
I have a module with several test case classes which each have
several test methods:
class Foo(unittest.TestCase):
def testMethod1(self):...
|
by: KemperR |
last post by:
Dear All,
may be some of you can help me with an XSLT example how to solve the
following challange.
For the XML below I want to find out the...
|
by: Fresh Air Rider |
last post by:
Hi SQL Gurus
Could anyone please explain how the following stored procedure can be
amended to retrieve Subordinates in alphabetical order ?
...
|
by: Kevin Newman |
last post by:
I've been toying with a namespace manager, and wanted to get some input.
So what do you think?
if (typeof com == 'undefined') var com = {};
if...
|
by: Bruce Hensley |
last post by:
I don't know how to begin on a query (SELECT statement) to find all the
tasks assigned to an arbitrary manager (say, staffID='JSmith') and her...
|
by: FrankEBailey |
last post by:
I've been reading up on Modified Preorder Tree Traversal and it's
definitely ideal for the kind of tree structures I need to model for my
company's...
|
by: the6campbells |
last post by:
for the conext of this example imagine a simple part table where a
part can only have a single parent. try to express a cte where the
result set...
|
by: Tem |
last post by:
I have a table that looks like this. Each row is a reply or a topic when
IsAReplyTo = 0
I need to write a query that pulls all rows of a hierarchy...
|
by: Simon Woods |
last post by:
Hi
I have this recursive function and I want to walk the inheritance
hierarchy to set field values ....
the generic T is constrainted as the...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...
| |