2

I am having a SQL function and I want to return a table, but I'm getting an error: A RETURN statement with a return value cannot be used in this context.

Here is my query:

 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 a.Category_Id=@CatId -- 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

And the error is:

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

2
  • 2
    Can you post the error that you're getting? It would help us identify the issue faster.. Commented Jun 18, 2013 at 7:59
  • A RETURN statement with a return value cannot be used in this context. Commented Jun 18, 2013 at 8:00

2 Answers 2

1

Your query is incorrect:

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 a.Category_Id=@CatId -- 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

you declare your table definition just before as

Sign up to request clarification or add additional context in comments.

Comments

1

--Transact-SQL Multistatement Table-valued Function Syntax

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
[ ; ]

Your modified function

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 a.Category_Id = @CatId -- 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 @Table select * from x;
  RETURN
END

2 Comments

Cannot perform alter on 'dbo.Fnc_GetParentCategories' because it is an incompatible object type.
He's right. He can't ALTER the old function into this one, he has to DROP and CREATE it, because the type of function changed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.