MSSQL 上标量 UDF 中的 NULL 参数
标量 UDF 的“RETURNS NULL ON NULL INPUT”选项(请参阅 CREATE FUNCTION) 如果参数为 null,则停止函数体执行并简单地返回 NULL。
也就是说,它短路了。
有谁知道它如何处理多个参数?
短路具有多个参数的函数调用是很有用的,比如第一个参数至少为 NULL。
当我有时间时,我将使用探查器来尝试跟踪 udf 调用。 我已经搜索过,但找不到任何东西..更有可能我可能没有使用正确的搜索词。
同时,有人有任何想法或经验吗?
来自其他 RDBMS 世界的答案也很受欢迎。这是一个 ANSI 设置,我在搜索中看到了 DB2 和 MySQL 的结果
编辑,基于评论:仅适用于非 CLR 函数
干杯 编辑
: 我不需要运行探查器。 哎哟! 这演示了以下行为:
CREATE FUNCTION dbo.ufnTest (
@dummy tinyint
)
RETURNS tinyint
WITH RETURNS NULL ON NULL INPUT
AS
BEGIN
RETURN 1
END
GO
SELECT dbo.ufnTest(0), dbo.ufnTest(NULL)
GO
ALTER FUNCTION dbo.ufnTest (
@dummy tinyint
)
RETURNS tinyint
--WITH RETURNS NULL ON NULL INPUT
AS
BEGIN
RETURN 1
END
GO
SELECT dbo.ufnTest(0), dbo.ufnTest(NULL)
GO
ALTER FUNCTION dbo.ufnTest (
@dummy tinyint,
@dummy2 tinyint
)
RETURNS tinyint
WITH RETURNS NULL ON NULL INPUT
AS
BEGIN
RETURN 1
END
GO
SELECT dbo.ufnTest(0, 2), dbo.ufnTest(NULL, 2), dbo.ufnTest(0, NULL)
GO
ALTER FUNCTION dbo.ufnTest (
@dummy tinyint,
@dummy2 tinyint
)
RETURNS tinyint
--WITH RETURNS NULL ON NULL INPUT
AS
BEGIN
RETURN 1
END
GO
SELECT dbo.ufnTest(0, 2), dbo.ufnTest(NULL, 2), dbo.ufnTest(0, NULL)
GO
The option "RETURNS NULL ON NULL INPUT" for a scalar UDF (see CREATE FUNCTION) stops the function body executing if the parameter is null and simply returns NULL.
That is, it short circuits.
Does anyone know how it handles multiple parameters?
It would be useful to short circuit a function call with multiple parameters, say if the first one is NULL at least.
When I have time, I'll use profiler to try and trace the udf calls.
I've searched but can't find anything.. more likely I may not have used the correct search terms.
In the meantime, does anyone have any ideas or experience?
Answers from the other RDBMS worlds are welcome too.. this is an ANSI setting and I saw results for DB2 and MySQL in my searches
Edit, based on comment: For non-CLR functions only
Cheers
S
Edit:
I don't need to run profiler. Doh!
This demonstrates the behaviour:
CREATE FUNCTION dbo.ufnTest (
@dummy tinyint
)
RETURNS tinyint
WITH RETURNS NULL ON NULL INPUT
AS
BEGIN
RETURN 1
END
GO
SELECT dbo.ufnTest(0), dbo.ufnTest(NULL)
GO
ALTER FUNCTION dbo.ufnTest (
@dummy tinyint
)
RETURNS tinyint
--WITH RETURNS NULL ON NULL INPUT
AS
BEGIN
RETURN 1
END
GO
SELECT dbo.ufnTest(0), dbo.ufnTest(NULL)
GO
ALTER FUNCTION dbo.ufnTest (
@dummy tinyint,
@dummy2 tinyint
)
RETURNS tinyint
WITH RETURNS NULL ON NULL INPUT
AS
BEGIN
RETURN 1
END
GO
SELECT dbo.ufnTest(0, 2), dbo.ufnTest(NULL, 2), dbo.ufnTest(0, NULL)
GO
ALTER FUNCTION dbo.ufnTest (
@dummy tinyint,
@dummy2 tinyint
)
RETURNS tinyint
--WITH RETURNS NULL ON NULL INPUT
AS
BEGIN
RETURN 1
END
GO
SELECT dbo.ufnTest(0, 2), dbo.ufnTest(NULL, 2), dbo.ufnTest(0, NULL)
GO
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是,如果任何参数为
NULL
,则指定RETURNS NULL ON NULL INPUT
的函数将短路。文档确实表明了这一点,尽管还不清楚并且似乎仅指的是到 CLR 函数。 (我认为这是 Microsoft 试图澄清
CREATE FUNCTION
中指定的NULL
行为将覆盖 CLR 方法上的OnNullCall
属性。)已在 SQL2005 和 SQL2008 中使用非 CLR 函数对此进行了测试,并且它完全按照预期短路。
Yes, a function that specifies
RETURNS NULL ON NULL INPUT
will short-circuit if any of its parameters areNULL
.The documentation does suggest this, although it's unclear and appears to be referring only to CLR functions. (I think this is Microsoft's attempt to clarify that the
NULL
behaviour specified inCREATE FUNCTION
will override theOnNullCall
attribute on the CLR method.)I've tested this with non-CLR functions in both SQL2005 and SQL2008 and it short-circuits exactly as expected.
您的链接中的解释是否足够,或者您是否正在寻找不同的方式?
is the explanation in your link sufficient, or were you looking for a different take?