Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL Server 2014,函数/过程中包含子句_Sql_Sql Server_Recursion_Recursive Query - Fatal编程技术网

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

SQL Server 2014,函数/过程中包含子句,sql,sql-server,recursion,recursive-query,Sql,Sql Server,Recursion,Recursive Query,是否可以在用户定义函数/用户定义过程中使用“with”子句 CREATE FUNCTION udf_UsersComments ( @Id INT ) RETURNS @UsersComments TABLE ( CommentTextFormatted NVARCHAR(MAX), DateCommented NVARCHAR(MAX), Username NVARCHAR(255), ParentCommentId INT, Id IN

是否可以在用户定义函数/用户定义过程中使用“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,16级,状态2,程序udf_用户建议,第13行 函数中包含的Select语句无法将数据返回到 客户


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

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 

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

我投票决定将这个问题作为离题题结束,因为OP懒得自己测试它。是一个更好的尝试,如果没有达到预期效果,询问行为。是的。如果您在执行此操作时遇到问题,请将您尝试过的代码添加到您的问题中。@DanGuzman donestill getting errors Msg 156,Level 15,State 1,Procedure udf_UsersComments,第8行关键字“WITH”附近的语法不正确。Msg 319,第15级,状态1,过程udf_UsersComments,第8行关键字“with”附近的语法不正确。如果此语句是公共表表达式、xmlnamespaces子句或更改跟踪上下文子句,则前一条语句必须以分号终止。Msg 102,第15级,状态1,过程udf_UsersComments,第43行“')附近语法不正确。Msg 102,级别15,状态31,过程udf_UsersComments,“开始”附近的语法不正确。@john--立即更新检查我仍然希望保留lineage@john我的血统结构是什么?我的意思是现在我失去了它的顺序+我得到/1和根的顺序