2013-06-18 5 views
2

SQL 함수가 있는데 테이블을 반환하려고하지만 오류가 발생합니다. A RETURN statement with a return value cannot be used in this context. 여기 SQL 함수에서 테이블을 반환하는 방법

내 쿼리입니다 :

ALTER FUNCTION [dbo].[Fnc_GetParentCategories]-- 21 
    ( 
@catId int 
) 
    Returns table 

As 
Begin 

Declare @Table table(Id int identity(1,1),Category_Id int,ParentId int); 
declare @cid int; 
WITH x AS (    
SELECT a.Category_Id, a.ParentId    
FROM t_Category a    
WHERE [email protected] -- enter dead node walking here    

UNION ALL    
SELECT b.Category_Id, b.ParentId    
FROM t_Category b    
JOIN x ON x.Category_Id =b.ParentId    
)    

    insert into @Table select * from x; 

return @Table 

end 

그리고 오류는 다음과 같습니다

A RETURN statement with a return value cannot be used in this context

+2

당신이지고있는 오류를 게시 할 수 방금 전에 테이블 정의를 선언? 문제를 더 빠르게 식별하는 데 도움이됩니다. –

+0

반환 값이있는 RETURN 문은이 컨텍스트에서 사용할 수 없습니다. – Cronaldo

답변

0

--Transact-SQL 다중 문 테이블 반환 함수 구문

CREATE FUNCTION [ schema_name. ] function_name 
([ { @parameter_name [ AS ] [ type_schema_name. ] parameter_data_type 
    [ = default ] [READONLY] } 
    [ ,...n ] 
    ] 
) 
RETURNS @return_variable TABLE <table_type_definition> 
    [ WITH <function_option> [ ,...n ] ] 
    [ AS ] 
    BEGIN 
     function_body 
     RETURN 
    END 
[ ; ] 

귀하의 수정 기능

,693,210
+0

호환되지 않는 개체 유형이므로 'dbo.Fnc_GetParentCategories'에서 alter를 수행 할 수 없습니다. – Cronaldo

+0

@Anilkumar M 당신이 틀렸나 봅니다. 다시 시도하십시오. –

1

귀하의 쿼리는 올바르지 않습니다

ALTER FUNCTION [dbo].[Fnc_GetParentCategories]-- 21 
(
    @catId int 
) 
Returns @Table table 
(
    Id int identity(1,1), 
    Category_Id int, 
    ParentId int 
) 
AS 
    Begin 

    WITH x AS 
    (
     SELECT a.Category_Id, a.ParentId 
     FROM t_Category a 
     WHERE [email protected] -- enter dead node walking here 

     UNION ALL 

     SELECT b.Category_Id, b.ParentId 
     FROM t_Category b 
     JOIN x ON x.Category_Id =b.ParentId 
    ) 

    insert into @Table select * from x; 

    return 

end 

당신은 as

관련 문제