没有输入参数的 SQL 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4849864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 09:07:18  来源:igfitidea点击:

SQL Function without input parameters

sqlsql-servertsql

提问by Sudantha

How to create a SQL Function withoutinput parameters

如何创建没有输入参数的 SQL 函数

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.

回答by Neil Knight

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

你错过了你()的。此外,它应该是RETURNS第一个RETURN

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

回答by Jakob Nilsson-Ehle

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

创建函数时,应使用RETURNS而非声明返回类型RETURN

回答by Sachin Shanbhag

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

它的()您在函数名称之后缺少,并且还在函数名称之后使用RETURNS

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

请参阅此链接 - http://msdn.microsoft.com/en-us/library/ms186755.aspx