T-SQL - 带有默认参数的函数 [英] T-SQL - function with default parameters

查看:29
本文介绍了T-SQL - 带有默认参数的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个脚本:

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

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

I want to use it in a procedure in this way:

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

但我收到错误:

为过程或函数 dbo.CheckIfSFExists 提供的参数数量不足.

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

为什么它不起作用?

推荐答案

你必须这样称呼它

SELECT dbo.CheckIfSFExists(23, default)

来自技术网:

当函数的参数有默认值时,关键字调用函数时必须指定 DEFAULT检索默认值.这种行为不同于使用存储过程中具有默认值的参数,其中省略该参数还暗示了默认值.一个例外行为是在使用 EXECUTE 调用标量函数时陈述.使用 EXECUTE 时,不需要 DEFAULT 关键字.

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.

这篇关于T-SQL - 带有默认参数的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆