2017-12-24 12 views
0

"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) 선택 문을 얻고있다.

+0

OP는 자체 테스트를 거치지 않기 때문에 오프닝으로이 질문을 닫으려고합니다. 시도하는 것이 더 좋은 생각이며 예상대로 작동하지 않으면 행동에 대해 물어보십시오. – danihp

+1

예. 문제가 발생하는 경우 질문에 시도한 코드를 추가하십시오. –

+0

@DanGuzman done – john

답변

1

인라인 테이블 값 함수로 만듭니다. 내가 멀티 라인 테이블 반환 함수의 인라인 대신

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 [email protected]) 

주를 선택한 이유를이 question 알고 확인, 나는 기능을 선택하는 동안 주문을 수행하는 결과에 다른 열을 추가했습니다. 당신은 함수 원래 문제에 대해서는

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

내부 TOP없이 Order by를 사용할 수 없습니다, 당신은 insert into @UsersComments가 누락되었습니다. CTE select에 레코드를 삽입해야합니다. @UsersComments

+0

메시지 156, 수준 15, 상태 1, 프로 시저 udf_UsersComments, 줄 8 키워드 'WITH'근처에 구문이 잘못되었습니다. 메시지 319, 수준 15, 상태 1, 프로 시저 udf_UsersComments, 줄 8 'with'키워드 근처의 구문이 잘못되었습니다. 이 문이 공통 테이블 식, xmlnamespaces 절 또는 변경 내용 추적 컨텍스트 절인 경우 이전 문은 세미콜론으로 끝나야합니다. 메시지 102, 수준 15, 상태 1, 프로 시저 udf_UsersComments, 줄 43 ')'근처에 구문이 잘못되었습니다. 메시지 102, 수준 15, 상태 31, 프로 시저 udf_UsersComments, 'BEGIN'근처의 구문이 잘못되었습니다. – john

+0

@john - 지금 수표 업데이트 –

+0

아직 계보의 구조를 유지하고 싶습니다. – john