tsql - T-SQL - 具有默认参数的函数

标签 tsql function default-parameters

我有这个脚本:

CREATE FUNCTION dbo.CheckIfSFExists(@param1 INT, @param2 BIT = 1 )
RETURNS BIT
AS
BEGIN
    IF EXISTS ( bla bla bla )
        RETURN 1;
    RETURN 0;
END
GO

我想以这种方式在程序中使用它:

IF dbo.CheckIfSFExists( 23 ) = 0
    SET @retValue = 'bla bla bla';

但我收到错误:

An insufficient number of arguments were supplied for the procedure or function dbo.CheckIfSFExists.

为什么不起作用?

最佳答案

你必须这样调用它

SELECT dbo.CheckIfSFExists(23, default)

来自Technet :

When a parameter of the function has a default value, the keyword DEFAULT must be specified when the function is called in order to retrieve the default value. This behaviour is different from using parameters with default values in stored procedures in which omitting the parameter also implies the default value. An exception to this behaviour is when invoking a scalar function by using the EXECUTE statement. When using EXECUTE, the DEFAULT keyword is not required.

关于tsql - T-SQL - 具有默认参数的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8358315/

相关文章:

sql - 如何将 TSQL 聚合函数转换为 Postgresql

sql-server - 如何解释 t-sql 死锁跟踪

sql - SET NOCOUNT ON 使用情况

Javascript if/else 语句未按预期工作

scala - 为什么无法指定 Scala varargs 参数的默认值?

sql-server - T-SQL 确定性 INT 转换

matlab - 如何在 MATLAB 中重新定义 .^ 运算符?

function - 具有 Fortran 函数的有组织的库

c++ - qt c++ new 类似的方法。我是重载方法、编写新方法还是使用默认参数?

C++数组默认参数,同时使函数重载