SQL Server 2014中包含函数/过程内的子句

问题描述 投票:0回答:1

是否可以在其中使用“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

我收到了这个错误

消息444,级别16,状态2,过程udf_UsersComments,第13行函数中包含的Select语句不能将数据返回给客户端。

sql sql-server recursion recursive-query
1个回答
1
投票

使其成为内联表值函数。检查这个question以了解为什么我选择内联而不是多行表值函数

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)

注意,我在结果中添加了另一列,以便在选择函数时进行排序。你不能在函数内部使用没有Order byTOP

select CommentTextFormatted, DateCommented, Username, ParentCommentId, id
from udf_UsersComments(1)--some id
order by ordercol 

关于你的原始问题,你缺少insert into @UsersComments。 CTE select应将记录插入@UsersComments

© www.soinside.com 2019 - 2024. All rights reserved.