我有一个存储过程,我想在下面的SELECT中使用,但我收到一个错误.
存储过程的输出是一个包含int类型列的表
select * from users where resCode in (EXEC getUserResselers 1)
-- stored procedure
create procedure getUserResselers
@userCode int
as
;WITH Directories AS (SELECT code FROM Resseler WHERE code =(select resselerCode from Users where code=@userCode)
UNION ALL SELECT d.code FROM Resseler d INNER JOIN Directories p ON d.parent = p.code)
SELECT * FROM Directories d
最佳答案 不支持从存储过程直接选择.但是,这种类型的操作是
table-valued user-defined function的一个很好的候选者,它可能看起来像:
请注意,您有拼写错误的“转销商”.我已经离开拼写错误,以便您可以使用现有架构进行测试.
CREATE FUNCTION getUserResselers
(
@UserCode INT
)
RETURNS TABLE AS
RETURN(
WITH Directories AS (
SELECT code FROM Reseller WHERE code = (select resselerCode from Users where code=@userCode)
UNION ALL SELECT d.code FROM Resseler d INNER JOIN Directories p ON d.parent = p.code
)
SELECT * FROM Directories
)
然后,您可以在查询中包含函数的连接,或者您可以继续使用IN,它应该类似于:
select * from users where resCode in (SELECT Code FROM getUserResselers(1));