Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Functions returing a table cannot declare variables? 1

Status
Not open for further replies.

Luzian

Programmer
Nov 27, 2005
103
US
In Microsoft SQL Server Management Studio Express:
Msg 102, Level 15, State 1, Procedure fn_GetMessages, Line 2
Incorrect syntax near 'RETURNS'.
Msg 178, Level 15, State 1, Procedure fn_GetMessages, Line 6
A RETURN statement with a return value cannot be used in this context.

In visual studio 2005:
Code:
Incorrect syntax near the keyword 'DECLARE'.

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

In either case, it seems the "DECLARE" statement causes the error. Removing that line of code proves it. It seems I can't do anything within a function returning a table, but return the table.

Here's the minimal query:
Code:
CREATE FUNCTION dbo.fn_GetRoles
(@parameter int)
RETURNS TABLE
AS
[red]DECLARE @some_variable int;[/red]

RETURN
(
    SELECT *
    FROM [Roles]
)
 
When creating a function that returns a table, you need to indicate what the structure of that table is. You also need to use Begin/End to define the body of the function.

Code:
CREATE FUNCTION dbo.fn_GetRoles
(@parameter int)
RETURNS [!]@output[/!] TABLE[!](Field1 Int, Field2 VarChar(20))[/!]
AS
Begin
  DECLARE @some_variable int;

  Insert Into @Output(Field1, Field2)
  Select Field1, Field2
  From   [Roles]

  RETURN
End

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
The syntax below in red doesn't work. Have you tried it?
Code:
CREATE FUNCTION dbo.fn_GetRoles
(@parameter int)
RETURNS @output TABLE(Field1 Int, Field2 VarChar(20))
AS
BEGIN
	DECLARE @some_variable int;

	[red]INSERT INTO @Output(Field1, Field2)
	SELECT Field1, Field2
	FROM [Roles][/red]

	RETURN
END
 
Does your [Roles] table have Field1 and Field1 columns? I used generic names because you didn't indicate what the field names are. You will need to replace Field1 and Field2 to your actual column names in the [roles] table. Also, in the RETURNS line, you'll have to specify the data types of the columns you want returned.

Trust me. I tried this. I tested this. It works. Once you specify the actual field names (with their corresponding data types, you should be able to use the function). If you want further assistance, run the following query and paste the results here.

Code:
Select Column_Name, Data_Type, Character_Maximum_Length 
from Information_Schema.Columns 
Where table_name = 'Roles'


-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
My fields are correct. I double checked my syntax, the RETURN statement I had was incorrect. Your syntax ir correct, sorry about that. Thanks
 
You're welcome. I'm glad you got this working.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top