I have spent about 10 hours on this issue so far trying everything that I can think of to work around this so any help would be greatly appreciated. I just feel like there should be simple solution here, I just may not be seeing the forest through the trees.
I am trying to find all employees unders a manager in an employees table. Manager being generic for anyone with direct reports; so a VP, director, or supervisor would have a mgrid (manager ID). The issue is recursing down to the lowest level employee under a VP, manager, or supervisor. So for a VP there would be a managers, supervisors, and employees. The employees table has an emplid (employee ID) and a mgrid (manager ID).
CODE:
declare @rowcount int -- exit when no more rows are
-- returned
declare @emplids varchar(8000)
set @emplids = '255,' -- initial manager ID (could be
-- VP, etc).
-- Recurs through adding emplid's for each level.
while @rowcount > 0 or @rowcount is null
begin
print 'begin: '+@emplids
select @emplids = @emplids + ltrim(str(emplid)) + ','
from dbo.temployees
where mgrid in (left(@emplids,(len(@emplids)-1)))
set @rowcount = @@rowcount
print 'end: '+@emplids
print @emplids
end
RESULT / ERROR MESSAGE:
begin: 255,
end: 255,67,79,124,299,418,460,685,
255,67,79,124,299,418,460,685,
begin: 255,67,79,124,299,418,460,685,
Server: Msg 245, Level 16, State 1, Line 8
Syntax error converting the varchar value '255,67,79,124,299,418,460,685' to a column of data type int.
The problem is that mgrid is an int and so SQL is trying to convert the in list to an int. This works on the first pass through because SQL can convert the 255 to an int of 255. It can not convert the longer in list though. Any thoughts? Work arounds?
Thanks!
Craig
I am trying to find all employees unders a manager in an employees table. Manager being generic for anyone with direct reports; so a VP, director, or supervisor would have a mgrid (manager ID). The issue is recursing down to the lowest level employee under a VP, manager, or supervisor. So for a VP there would be a managers, supervisors, and employees. The employees table has an emplid (employee ID) and a mgrid (manager ID).
CODE:
declare @rowcount int -- exit when no more rows are
-- returned
declare @emplids varchar(8000)
set @emplids = '255,' -- initial manager ID (could be
-- VP, etc).
-- Recurs through adding emplid's for each level.
while @rowcount > 0 or @rowcount is null
begin
print 'begin: '+@emplids
select @emplids = @emplids + ltrim(str(emplid)) + ','
from dbo.temployees
where mgrid in (left(@emplids,(len(@emplids)-1)))
set @rowcount = @@rowcount
print 'end: '+@emplids
print @emplids
end
RESULT / ERROR MESSAGE:
begin: 255,
end: 255,67,79,124,299,418,460,685,
255,67,79,124,299,418,460,685,
begin: 255,67,79,124,299,418,460,685,
Server: Msg 245, Level 16, State 1, Line 8
Syntax error converting the varchar value '255,67,79,124,299,418,460,685' to a column of data type int.
The problem is that mgrid is an int and so SQL is trying to convert the in list to an int. This works on the first pass through because SQL can convert the 255 to an int of 255. It can not convert the longer in list though. Any thoughts? Work arounds?
Thanks!
Craig