SQL Server 2014 内含子句 function/procedure

SQL Server 2014 with clause inside function/procedure

是否可以使用其中的 "with" 子句创建用户定义的函数/用户定义的过程?

CREATE FUNCTION udf_UsersComments (
    @Id INT
    )
RETURNS @UsersComments TABLE (
    CommentTextFormatted NVARCHAR(MAX),
    DateCommented NVARCHAR(MAX),
    Username NVARCHAR(255),
    ParentCommentId INT,
    Id INT
    )
AS
BEGIN
WITH UpperHierarchy (Id, QuestionId, CommentText, ParentCommentId, DateCommented, UserId, HierarchyOrder, 
     lineage) 
     AS (SELECT com.Id,
                com.QuestionId, 
                com.CommentText, 
                com.ParentCommentId,
                com.DateCommented,
                com.UserId,
                0                          AS HierarchyOrder, 
                Cast ('/' AS VARCHAR(255)) AS Lineage 
         FROM   Comments AS com 
         WHERE  com.ParentCommentId IS NULL AND IsDeleted=0
         UNION ALL
         (SELECT com.Id,
                com.QuestionId,
                com.CommentText, 
                com.ParentCommentId,
                com.DateCommented,
                com.UserId,
                HierarchyOrder + 1, 
                Cast(lineage + Ltrim(Str(com.ParentCommentId, 6, 0)) 
                     + '/' AS VARCHAR(255)) 
         FROM   Comments AS com 
                INNER JOIN UpperHierarchy AS parent 
                        ON com.ParentCommentId = parent.Id
                        WHERE com.IsDeleted=0))

SELECT CommentTextFormatted, DateCommented, U.Username, ParentCommentId, Com.id
FROM Questions AS Q
INNER JOIN 
    (SELECT Space(HierarchyOrder*5) + CommentText AS CommentTextFormatted, Id, QuestionId, ParentCommentId, DateCommented, UserId, lineage
    FROM   UpperHierarchy) AS Com
ON Com.QuestionId=Q.Id
INNER JOIN Users AS U
ON U.Id=Com.UserId
WHERE Q.Id=@Id
ORDER  BY lineage + Ltrim(Str(Q.Id, 6, 0))
RETURN
END
GO

我收到了这个错误

Msg 444, Level 16, State 2, Procedure udf_UsersComments, Line 13 Select statements included within a function cannot return data to a client.

将其设为内联 table 值函数。检查此 question 以了解为什么我选择内联而不是多行 table 值函数

CREATE FUNCTION udf_UsersComments (
    @Id INT
    )
RETURNS TABLE 
AS
Return(
WITH UpperHierarchy (Id, QuestionId, CommentText, ParentCommentId, DateCommented, UserId, HierarchyOrder, 
     lineage) 
     AS (SELECT com.Id,
                com.QuestionId, 
                com.CommentText, 
                com.ParentCommentId,
                com.DateCommented,
                com.UserId,
                0                          AS HierarchyOrder, 
                Cast ('/' AS VARCHAR(255)) AS Lineage 
         FROM   Comments AS com 
         WHERE  com.ParentCommentId IS NULL AND IsDeleted=0
         UNION ALL
         (SELECT com.Id,
                com.QuestionId,
                com.CommentText, 
                com.ParentCommentId,
                com.DateCommented,
                com.UserId,
                HierarchyOrder + 1, 
                Cast(lineage + Ltrim(Str(com.ParentCommentId, 6, 0)) 
                     + '/' AS VARCHAR(255)) 
         FROM   Comments AS com 
                INNER JOIN UpperHierarchy AS parent 
                        ON com.ParentCommentId = parent.Id
                        WHERE com.IsDeleted=0))

SELECT CommentTextFormatted, DateCommented, U.Username, ParentCommentId, Com.id,ordercol = lineage + Ltrim(Str(Q.Id, 6, 0))
FROM Questions AS Q
INNER JOIN 
    (SELECT Space(HierarchyOrder*5) + CommentText AS CommentTextFormatted, Id, QuestionId, ParentCommentId, DateCommented, UserId, lineage
    FROM   UpperHierarchy) AS Com
ON Com.QuestionId=Q.Id
INNER JOIN Users AS U
ON U.Id=Com.UserId
WHERE Q.Id=@Id)

请注意,我在结果中添加了另一列以在选择函数时进行排序。在函数

中没有 TOP 就不能使用 Order by
select CommentTextFormatted, DateCommented, Username, ParentCommentId, id
from udf_UsersComments(1)--some id
order by ordercol 

关于你原来的问题,你遗漏了insert into @UsersComments。 CTE select 应将记录插入 @UsersComments