AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 290663
Accepted
JohnG
JohnG
Asked: 2021-04-29 10:59:31 +0800 CST2021-04-29 10:59:31 +0800 CST 2021-04-29 10:59:31 +0800 CST

一个脚本中的多个 Set Quoted_identifier 采用最后一个?

  • 772

我正在使用 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,插入无法说明我引用的标识符没有正确设置,尽管它设置在TRYto的开头ON。

sql-server t-sql
  • 2 2 个回答
  • 188 Views

2 个回答

  • Voted
  1. Charlieface
    2021-04-29T16:13:49+08:002021-04-29T16:13:49+08:00

    这是设计使然:

    对于顶级 ad-hoc 批处理解析开始使用会话的当前设置QUOTED_IDENTIFIER。在解析批处理时,任何出现的SET QUOTED_IDENTIFIER都会从那时起更改解析行为,并为会话保存该设置。所以在批处理解析并执行后,会话的QUOTED_IDENTIFER设置将根据SET QUOTED_IDENTIFIER批处理中最后一次出现的设置。

    因此, 的行为QUOTED_IDENTIFIER取决于批处理的解析,而不是执行。解析批处理开始时的默认值来自当前的连接设置。

    @@OPTIONS仅向您显示当前默认值,如果解析了批处理,则将使用该默认值。这就是为什么 SSMS 总是放在SET一个单独的批次中。

    的执行与SET的行为无关@@OPTIONS,除了更改以后批次的默认值。


    您可以在 db-fiddle 中看到这一点

    set QUOTED_IDENTIFIER  on;
    create table "select"(i int);
    drop table "select";
    GO
    
    set QUOTED_IDENTIFIER  off;
    create table "select"(i int);
    drop table "select";
    --  Incorrect syntax near 'select'.
    GO
    
    set QUOTED_IDENTIFIER  off;
    if (1=0)
    begin
        set QUOTED_IDENTIFIER  on;
    end
    create table "select"(i int);
    drop table "select";
    GO
    
    set QUOTED_IDENTIFIER  on;
    if (1=0)
    begin
        set QUOTED_IDENTIFIER  off;
    end
    create table "select"(i int);
    drop table "select";
    -- Incorrect syntax near 'select'.
    GO
    
    • 8
  2. Best Answer
    Ronaldo
    2021-04-29T12:51:25+08:002021-04-29T12:51:25+08:00

    Charlieface的回答似乎已经适当地解释了您在使用时描述的行为的原因SET QUOTED_IDENTIFIER,所以如果您需要在 SP 中使用它,我将留下一个您可以使用的方法:

    建议的解决方案

    在另一个过程中隔离您的批处理的一部分,并从QUOTED_IDENTIFIER需要OFF这样的过程中调用它:db<>fiddle - 在您的计算机上运行 2 个不同的会话,因为我无法使其在 dbfiddle 上正常运行。

    • 0

相关问题

  • SQL Server - 使用聚集索引时如何存储数据页

  • 我需要为每种类型的查询使用单独的索引,还是一个多列索引可以工作?

  • 什么时候应该使用唯一约束而不是唯一索引?

  • 死锁的主要原因是什么,可以预防吗?

  • 如何确定是否需要或需要索引

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve