为什么 SQL 服务器存储过程选择加密数据到变量失败

Why SQL Server stored procedure selecting encrypted data into variable fails

从加密列(Always Encrypted)中选择数据到变量的存储过程失败并出现错误

Cannot continue the execution because the session is in the kill state

如果 XACT_ABORT 设置为开启。

删除 SET XACT_ABORT ON; 行使存储过程完美运行,但不清楚它是如何关联的。

完全删除变量也会修复一个错误。

环境:

存储过程:

CREATE PROCEDURE [SomeStoredProcedure]
WITH EXECUTE AS OWNER
AS
BEGIN
    SET NOCOUNT ON;
    SET XACT_ABORT ON;

    DECLARE @EncryptedValue VARBINARY(4096);

    SELECT TOP 1
        @EncryptedValue = [someencryptedcolumn]
    FROM
        [sometable];

    SELECT @EncryptedValue

    RETURN 0;
END;

Table声明:

CREATE TABLE [sometable]
(
    [someencryptedcolumn] [varbinary](4096)
                          ENCRYPTED WITH
                          (
                            COLUMN_ENCRYPTION_KEY = [CEK1],
                            ENCRYPTION_TYPE = Randomized,
                            ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256'
                           )
                           NULL
)

示例数据:

INSERT INTO [sometable] ([someencryptedcolumn])
VALUES (NULL)

调用存储过程:

EXEC [SomeStoredProcedure];

错误:

Msg 596, Level 21, State 1, Line 29
Cannot continue the execution because the session is in the kill state.

Msg 0, Level 20, State 0, Line 29
A severe error occurred on the current command. The results, if any, should be discarded.

我没有找到关于 XACT_ABORT 和 Always Encrypted 如何关联的参考资料。

我还检查了 SQL 服务器日志,但没有找到关于该问题的其他信息。

更新:

Link to the registered SQL Server bug

这是产品中的错误。您应该首先尝试安装最新的 SP/CU 以查看它是否已经修复,如果没有,请向 Microsoft 报告。

我也可以在 SQL Server 2017 RTM 上重现这个。我还没有尝试安装最新的 CU 以查看是否有改进。

它不特定于 XACT_ABORT。您还会看到与其他设置选项相同的情况。比如

  • SET DATEFIRST 5
  • SET ANSI_NULLS OFF.

当它们存在时,它最终会调用 sqllang.dll!CEnvColEncryptionKey::XretSchemaChanged 两次,而第二次它最终会尝试取消引用空指针并失败并返回 Access violation reading location 0x0000000000000000.

抛出错误(by SELECT @EncryptedValue)时的调用栈如下