Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Function without input parameters

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.
like image 766
Sudantha Avatar asked Jan 31 '11 10:01

Sudantha


2 Answers

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
like image 98
Neil Knight Avatar answered Sep 29 '22 19:09

Neil Knight


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

like image 31
Jakob Nilsson-Ehle Avatar answered Sep 29 '22 18:09

Jakob Nilsson-Ehle