我有一个查询,我在员工表中查找用户,并递归查看记录,找出报告我们的源输入的每个人。
我的问题是,在下面的情况中,@QID
Id希望能够传递多个值,而不是传递单个值。我试图将其修改为WHERE QID IN ('user1', 'user2')
,但它始终只返回它在IN
子句中找到的第一个用户的数据
WITH empList(mgrQID, QID, NTID, FullName, lvl, countOfDirects)
AS
(
SELECT TOP 1 mgrQID, QID, NTID, FirstName+' '+LastName, 0, CountOfDirects
FROM dbo.EmployeeTable
WHERE QID = @QID
UNION ALL
SELECT b.mgrQID, b.QID, b.NTID, b.FirstName+' '+b.LastName, lvl+1, b.countOfDirects
FROM empList a
CROSS APPLY dbo.Fetch_Directs_by_qid(a.QID)b
)
答案 0 :(得分:0)
摆脱TOP 1并像这样做。如果您有重复值,请将分组添加到锚点查询或DISTINCT
DECLARE @QID TABLE (val VARCHAR(100));
INSERT INTO @QID VALUES ('user1'),('user2')
WITH empList(mgrQID, QID, NTID, FullName, lvl, countOfDirects)
AS
(
SELECT mgrQID, QID, NTID, FirstName+' '+LastName, 0, CountOfDirects
FROM dbo.EmployeeTable
WHERE QID IN (SELECT * FROM @QID)
UNION ALL
SELECT b.mgrQID, b.QID, b.NTID, b.FirstName+' '+b.LastName, lvl+1, b.countOfDirects
FROM empList a
CROSS APPLY dbo.Fetch_Directs_by_qid(a.QID)b
)