11

How to create a SQL Function without input parameters

Im geting a error for following code

create function function_name 
RETURN datetime AS 
BEGIN 
DECLARE @var datetime
SELECT  @var=CURRENT_TIMESTAMP 
RETURN @var
END

Error

> 
> Msg 156, Level 15, State 1, Procedure
> fx_getcurrent_date, Line 2 Incorrect
> syntax near the keyword 'RETURN'. Msg
> 178, Level 15, State 1, Procedure
> fx_getcurrent_date, Line 7 A RETURN
> statement with a return value cannot
> be used in this context.

3 Answers 3

27

You are missing your ()'s. Also, it should be RETURNS for the first RETURN.

CREATE FUNCTION function_name
(
) 
RETURNS DATETIME 
AS 
BEGIN 
    DECLARE @var datetime 
    SELECT @var=CURRENT_TIMESTAMP 
    RETURN @var 
END
Sign up to request clarification or add additional context in comments.

Comments

4

When you create a function, the return type should be declared with RETURNS, not RETURN.

Comments

2

Its the () you are missing after function name and also use RETURNS after function name.

Refer this link - http://msdn.microsoft.com/en-us/library/ms186755.aspx

Comments

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.