IN子句中的SQL逗号列表

时间:2018-02-13 21:11:56

标签: sql-server

所以构建了下面的函数,它获得了一个列表,例如1,2,3,4,5等

ALTER FUNCTION [dbo].[fnGetID] (@CustID INT)
RETURNS VARCHAR(8000)
AS
BEGIN
    DECLARE @CustList VARCHAR(8000)

    SELECT @CustList = (SELECT Id + ', ' AS [text()] 
                        FROM Customer
                        WHERE Id < @CustID
                        ORDER BY Id 
                        FOR XML PATH('') )      

    RETURN LEFT(@CustList,(LEN(@CustList) -1))
END

然后我有一个查询

SELECT *
FROM Customer
WHERE Id IN (fnGetID(10))

我的结果一无所获。我知道我的结果会回归到&#39; 1,2,3,4&#39;它应该在哪里&#39; 1&#39;,&#39; 2&#39;,&#39; 3&#39;,&#39; 4&#39;

无论如何我能达到正确的结果吗?

0 个答案:

没有答案