SQL Server 2019 将 Base64 编码打破为标量值函数

SQL Server 2019 breaks Base64 encoding as a Scalar-valued Function

我有一个标量值函数,多年来一直在我的数据库中使用。它总是工作正常。我最近将数据库移动到 SQL Server 2019,当我将兼容级别更改为 SQL Server 2019 (150) 时,我的功能像硝化甘油一样爆炸。将兼容性级别恢复到 SQL Server 2017 (140) 允许一切正常运行。

这是函数:

CREATE FUNCTION [dbo].[Authentication_SHA1ToBase64](@source AS varchar(8000))
RETURNS varchar(8000)
BEGIN

    DECLARE
        @bin    varbinary(8000),
        @result varchar(8000)

    SET @bin = CONVERT(varbinary(8000), @source)

    RETURN CAST(N'' as xml).value('xs:base64Binary(xs:hexBinary(sql:variable("@bin")))', 'varchar(8000)')

END

在 SQL Server 2019 (150) 兼容级别,以下内容:

SELECT [dbo].[Authentication_SHA1ToBase64]('0x640AB2BAE07BEDC4C163F679A746F7AB7FB5D1FA')

错误如下:

Msg 596, Level 21, State 1, Line 0
Cannot continue the execution because the session is in the kill state.
Msg 0, Level 20, State 0, Line 0
A severe error occurred on the current command.  The results, if any, should be discarded.

奇怪的是,如果我只是执行与查询相同的操作:

DECLARE
    @bin    varbinary(8000)

SET @bin = CONVERT(varbinary(8000), '0x640AB2BAE07BEDC4C163F679A746F7AB7FB5D1FA')

SELECT [Result]=CAST(N'' as xml).value('xs:base64Binary(xs:hexBinary(sql:variable("@bin")))', 'varchar(8000)')

我得到了预期的结果:

Result
--------------------------------------------------------
MHg2NDBBQjJCQUUwN0JFREM0QzE2M0Y2NzlBNzQ2RjdBQjdGQjVEMUZB

也许有一些内部异步问题破坏了功能?有人知道 SQL Server 2019 中是否有更新的方法转换为 base64?

更新:@@VERSION 是:Microsoft SQL Server 2019 (RTM-GDR) (KB4583458) - 15.0.2080.9 (X64) 2020 年 11 月 6 日 16:50:01 版权所有 (C) 2019 Microsoft Corporation Windows Server 2019 Datacenter 10.0(内部版本 17763:)(管理程序)上的标准版(64 位)

我更新到 CU9,将兼容性修改回 150,并且有效! Microsoft SQL Server 2019 (RTM-CU9) (KB5000642) - 15.0.4102.2 (X64) 2021 年 1 月 25 日 20:16:12 版权所有 (C) 2019 Microsoft Corporation Standard Edition(64 位)在 Windows Server 2019 Datacenter 10.0(内部版本 17763:)(管理程序)