1

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.

0

1 Answer 1

5

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.

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

4 Comments

I have multiple lines in function thats why i used begin and end.i have
@TanuJain I mean multistatement. Of course SELECT could be broken to multiple lines for readability
No i mean i have to do processing inside function like creating dynamic query and then executing it using EXEC(@SQL). Without begin and end how should i do it.
@TanuJain You cannot execute dynamic SQL inside function. Period. Check Call dynamic SQL from function

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.