1

从 SQL Server 2016 到版本 2019,功能发生了一些变化。

这是测试功能:

create function [dbo].[TestFunction]
(
    @input uniqueidentifier
)
returns uniqueidentifier
as
begin
    select top 0 @input = id from randomTable;

    return
    (
        select  @input
    )
end

如果我在 SQL Server 2016 中尝试此代码,结果是输入值,但在 SQL Server 2019 中,结果为空。

有人可以指出我改变的正确方向吗?

4

1 回答 1

2

返回的 NULL 是由于标量 udf 内联:

MS SQL 2019 中的 UDF 内出现意外的 @@rowcount 行为

如果您希望函数利用内联并返回正确的结果,您可以将其重构为:

create function [dbo].[TestFunction]
(
    @input uniqueidentifier
)
returns uniqueidentifier
--WITH INLINE = ON
as
begin
    --select top (0) @input= id from randomTable

    return
    (
        isnull((select top (0) id from randomtable), @input)
    )
end
于 2020-01-11T09:53:02.563 回答