2011-01-31 118 views
7

如何沒有輸入參數SQL功能參數

即時通訊歌廳一個錯誤下面的代碼創建一個SQL函數

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

錯誤

> 
> 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. 

回答

17

你缺少你的()的。此外,它應該是RETURNS第一個RETURN

CREATE FUNCTION function_name 
(
) 
RETURNS DATETIME 
AS 
BEGIN 
    DECLARE @var datetime 
    SELECT @var=CURRENT_TIMESTAMP 
    RETURN @var 
END 
4

當你創建一個函數,返回類型應與RETURNS,不RETURN聲明。