简体   繁体   English

SQL Server 2014在函数/过程中带有子句

[英]SQL Server 2014 with clause inside function/procedure

Is it possible to make a user defined function /user defined procedure with the "with" clause inside it? 是否可以在其中带有“ 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

And I am getting this error 我收到这个错误

Msg 444, Level 16, State 2, Procedure udf_UsersComments, Line 13 Select statements included within a function cannot return data to a client. 消息444,级别16,状态2,过程udf_UsersComments,第13行函数中包含的Select语句无法将数据返回给客户端。

Make it as a Inline table valued function. 使它成为内联表值函数。 Check this question to know why I chose inline instead of multi line table valued function 检查此问题以了解为什么我选择内联而不是多行表值函数

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)

Note, I have added another column in result to do the ordering while selecting the function. 注意,我在结果中添加了另一列以在选择函数时进行排序。 You cannot use Order by without TOP inside a function 您不能在函数中没有TOP Order by情况下使用Order by

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

Regarding your original issue, you are missing insert into @UsersComments . 关于您的原始问题,您没有insert into @UsersComments CTE select should insert the records into @UsersComments CTE select应该将记录插入@UsersComments

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM