4

我有一个存储过程,我想在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
4

1 回答 1

2

不支持从存储过程中直接选择。但是,这种类型的操作非常适合表值用户定义函数,它可能看起来像:

请注意,您拼错了“经销商”。我留下了拼写错误,以便您可以使用现有架构进行测试。

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));
于 2012-12-15T21:25:09.753 回答