SQL SQL函数作为默认参数值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/470664/
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 00:51:08  来源:igfitidea点击:

SQL function as default parameter value?

sqlsql-serverstored-procedures

提问by user58044

I tried changing a default parameter value with this:

我尝试使用以下方法更改默认参数值:

ALTER PROCEDURE [dbo].[my_sp]
@currentDate datetime = GETDATE()

and all the SQL pre-compiler gave me was this error:

并且所有 SQL 预编译器给我的是这个错误:

Msg 102, Level 15, State 1, Procedure my_sp, Line 8 Incorrect syntax near '('.

消息 102,级别 15,状态 1,过程 my_sp,第 8 行 '(' 附近的语法不正确。

I have already created the procedure. (I'm not sure if that's relevant.) I was using a null default value and checking for that later, but that doesn't seem proper. Can I do this in one line?

我已经创建了程序。(我不确定这是否相关。)我使用的是空默认值并稍后检查,但这似乎不正确。我可以在一行中做到这一点吗?



更新:我要离开了 MSDN's description of stored procedure parametersMSDN对存储过程参数的描述

[ = default ] Is a default value for the parameter. If a default value is defined, the function can be executed without specifying a value for that parameter.

Note:
Default parameter values can be specified for CLR functions except for the varchar(max) and varbinary(max) data types.

When a parameter of the function has a default value, the keyword DEFAULT must be specified when the function is called to retrieve the default value. This behavior is different from using parameters with default values in stored procedures in which omitting the parameter also implies the default value.

[ = default ] 是参数的默认值。如果定义了默认值,则无需为该参数指定值即可执行该函数。

注意:
可以为 CLR 函数指定默认参数值,但 varchar(max) 和 varbinary(max) 数据类型除外。

当函数的参数有默认值时,在调用函数检索默认值时必须指定关键字DEFAULT。这种行为不同于在存储过程中使用带有默认值的参数,在存储过程中省略参数也意味着默认值。

Am I reading this wrong?

我读错了吗?

Many thanks.

非常感谢。

回答by Brian Kim

Default value for stored procedures parameter have to be constants. You'd need to do the following...

存储过程参数的默认值必须是常量。您需要执行以下操作...

ALTER Procedure [dbo].[my_sp]
@currentDate datetime = null
AS
IF @currentDate is null
SET @currentDate = getdate()

回答by Otávio Décio

I don't think that is possible, you have to use a literal (constant) value as the default.

我认为这是不可能的,您必须使用文字(常量)值作为默认值。

However you can do this:

但是你可以这样做:

Set @currentDate = Coalesce(@currentDate , GetDate())

回答by Otávio Décio

You can try as follow:

您可以尝试如下:

Set @CurrentDate=IsNull(@CurrentDate,GetDate())

回答by Bill Karwin

I infer you're using Microsoft SQL Server from the square brackets in your example.

我从示例中的方括号推断您正在使用 Microsoft SQL Server。

From MSDN:

MSDN

Only a constant value, such as a character string; a scalar function (either a system, user-defined, or CLR function); or NULL can be used as a default.

只有一个常量值,如字符串;标量函数(系统、用户定义或 CLR 函数);或 NULL 可用作默认值。

The function GETDATE()returns a different value from time to time, so it is not a constant expression.

该函数GETDATE()不时返回不同的值,因此它不是常量表达式。

回答by SQLMenace

That value is not deterministic and cannot be used

该值不是确定性的,不能使用

回答by MarlonRibunal

Suggestion:

建议:

Set the default to NULL

将默认设置为 NULL

Do the Default GETDATE()in the front end.

GETDATE()在前端做默认。