我正在使用 SQL Server 2019,但发现了一个奇怪的行为。研究并没有让我更进一步。
有人可以解释这种行为吗?
SET QUOTED_IDENTIFIER ON;
if ((256 & @@options) = 256) print '1- quoted_identifier is on' else print '1- quoted_identifier is off';
BEGIN TRY
if ((256 & @@options) = 256) print '2- quoted_identifier is on' else print '2- quoted_identifier is off';
END TRY
BEGIN CATCH
if ((256 & @@options) = 256) print '3- quoted_identifier is on' else print '3- quoted_identifier is off';
-- SET QUOTED_IDENTIFIER OFF
-- if ((256 & @@options) = 256) print '4- quoted_identifier is on' else print '4- quoted_identifier is off';
END CATCH
回报:
1- quoted_identifier is on
2- quoted_identifier is on
但以下代码:
SET QUOTED_IDENTIFIER ON;
if ((256 & @@options) = 256) print '1- quoted_identifier is on' else print '1- quoted_identifier is off';
BEGIN TRY
if ((256 & @@options) = 256) print '2- quoted_identifier is on' else print '2- quoted_identifier is off';
END TRY
BEGIN CATCH
if ((256 & @@options) = 256) print '3- quoted_identifier is on' else print '3- quoted_identifier is off';
SET QUOTED_IDENTIFIER OFF
if ((256 & @@options) = 256) print '4- quoted_identifier is on' else print '4- quoted_identifier is off';
END CATCH
回报:
1- quoted_identifier is off
2- quoted_identifier is off
即使它没有进入捕获!我肯定错过了什么。
我什至能够将代码简化为最简单的:
SET QUOTED_IDENTIFIER ON;
if ((256 & @@options) = 256) print '1- quoted_identifier is on' else print '1- quoted_identifier is off';
SET QUOTED_IDENTIFIER OFF
if ((256 & @@options) = 256) print '2- quoted_identifier is on' else print '2- quoted_identifier is off';
结果:
1- quoted_identifier is off
2- quoted_identifier is off
我有一些使用 的代码FOR XML
,这需要我将带引号的标识符设置为 ON,但无论 XML 部分成功还是失败,我都需要将其设置回 OFF。你会怎么做?
我的测试表明,如果我在SET QUOTED_IDENTIFIER
中关闭 to CATCH
,插入无法说明我引用的标识符没有正确设置,尽管它设置在TRY
to的开头ON
。