Tanu Jain
Tanu Jain

Reputation: 107

Incorrect syntax near begin in sql server while creating function

I am stuck with a very petty issue that I am unable to resolve.

CREATE FUNCTION dbo.fntblPsmHaendlerDailyCostsinfodump(@pDateString varchar(8), @HaendlerID int, @TableName varchar(100),@CountryID int)
RETURNS table
AS BEGIN
RETURN select top 10 * from tblcountry
END
GO

This is giving me an error -

'Incorrect syntax near begin'.

I am not able to identify why it is giving the error.

Upvotes: 1

Views: 2286

Answers (1)

Lukasz Szozda
Lukasz Szozda

Reputation: 176124

Correct syntax for inline UDF:

Inline user-defined functions follow these rules:

  • There is no function_body delimited by BEGIN and END.
  • The RETURN clause contains a single SELECT statement in parentheses.
CREATE FUNCTION dbo.fntblPsmHaendlerDailyCostsinfodump(
     @pDateString varchar(8),
     @HaendlerID int,
     @TableName varchar(100),
     @CountryID int)
RETURNS table
AS
RETURN (select top 10 * from tblcountry);
GO

BEGIN and END are necessary for multistatement UDFs.

Upvotes: 5

Related Questions