"with"절이있는 사용자 정의 함수/사용자 정의 프로 시저를 만들 수 있습니까?함수/프로 시저 내부에 절이있는 SQL Server 2014
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 [email protected]
ORDER BY lineage + Ltrim(Str(Q.Id, 6, 0))
RETURN
END
GO
그리고이 오류
메시지 444, 수준 16, 상태 2, 프로 시저 udf_UsersComments, 일렬로 클라이언트에 데이터를 반환 할 수없는 기능에 포함 (13) 선택 문을 얻고있다.
OP는 자체 테스트를 거치지 않기 때문에 오프닝으로이 질문을 닫으려고합니다. 시도하는 것이 더 좋은 생각이며 예상대로 작동하지 않으면 행동에 대해 물어보십시오. – danihp
예. 문제가 발생하는 경우 질문에 시도한 코드를 추가하십시오. –
@DanGuzman done – john