插入记录时 SET QUOTED IDENTIFIER 应为 ON

发布于 2024-07-26 23:27:14 字数 423 浏览 12 评论 0原文

我陷入了 SQL Server 2005 的一个相当奇怪的问题,它抛出

“插入记录时应启用 SET QUOTED IDENTIFIER”

(用作 SP)到特定表。 这之前工作得很好,但随机抛出这个错误。

我已经验证了SP。 我们没有在里面手动指定 SET QUOTED IDENTIFIER 设置,所以它必须默认为 ON。

有人可以澄清可能是什么问题吗?

该表必须使用 SET QUOTED IDENTIFIER ON 创建,对吗? 我还没有检查表脚本。

我观察到这个问题仅发生在 SP 对日期列(modifiedAt)进行插入或更新时...示例值是“2009-08-10 06:43:59:447”..

是否有问题传递的值?

I am stuck in a rather strange problem with SQL Server 2005, which throws

"SET QUOTED IDENTIFIER should be on when inserting record"

(using as SP) to the particular table. This worked fine earlier but is throwing this error randomly.

I have verified the SP. We didn't manually specify SET QUOTED IDENTIFIER settings inside, so it must be ON by default.

Can someone clarify what could be the problem?

The table must be created with SET QUOTED IDENTIFIER ON right? I didn't check the table script yet.

I have observed that this problem only occur with the SPs doing insert or update on a date column (modifiedAt)... A sample value is '2009-08-10 06:43:59:447'..

Is there a problem with the values passed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无妨# 2024-08-02 23:27:14

经过长期的努力,我们终于解决了这个问题。 我只是想分享一下原因。

我们的构建团队维护一个单独的内部工具来部署脚本,该工具在内部触发SQLCMD(shell)实用程序在数据库中执行T-SQL脚本。

罪魁祸首是:默认情况下,在 SQLCMD 模式下运行时,QUOTED_IDENTIFIEROFF

通过此工具运行的每个脚本都是使用 创建的引用标识符关闭。 我们是唯一使用索引视图的模块。 您在我之前的帖子中熟悉的所有其余故事:(

注意:我将投票给每个人的帖子有用。

After a long struggle we were able to fix this problem. I just wanted to share the reason.

Our build team maintains a separate in-house tool to deploy scripts, which internally triggers the SQLCMD (shell) utility to execute T-SQL scripts in a db.

Here is the culprit: by default, QUOTED_IDENTIFIER is OFF when running in SQLCMD mode!

Every script run through this tool is created with QUOTED IDENTIFIER OFF. We are the only module which uses indexed views. All the remaining stories you know well in my previous posts :(

NOTE: I am going to vote everyone's post as useful.

不醒的梦 2024-08-02 23:27:14

编写存储过程的脚本,确保/更改 SET 选项,运行 ALTER PROC 以确保设置 SET QUOTED IDENTIFIER ON。

为什么?

“SET QUOTED IDENTIFIER”的设置是在创建存储过程时定义的,并且对于表始终为“ON”。 来源,BOL

创建表时,QUOTED
IDENTIFIER 选项始终存储为
在表的元数据中为 ON,即使
当表被设置时,选项设置为 OFF
创建。

创建存储过程时,
SET QUOTED_IDENTIFIER 和 SET
捕获 ANSI_NULLS 设置并
用于后续调用
该存储过程。

连接的默认值可以在服务器级别(sp_configure '用户选项')或数据库级别(ALTER DATABASE)定义。 对于 SSMS,它位于“工具..选项..查询执行..SQL Server..ANSI”下。 它也是客户端库的默认设置(DB-LIb 除外)。

现在,如果您打开 SSMS 查询窗口并开始输入“CREATE PROC..”,那么它会在您运行代码时使用 SSMS 设置。

并且 SET QUOTED IDENTIFIER 不能在存储过程内的运行时设置。 在你不同意之前给我看看参考资料...来自上面的 MS BOL 链接:

当在存储的内部执行时
程序,SET的设置
QUOTED_IDENTIFIER 未更改。

您必须努力工作才能在关闭状态下运行任何代码...因此最可能的解决方法是更改​​或重新创建存储过程。

Script the stored proc, ensure/change SET options, run the ALTER PROC to ensure SET QUOTED IDENTIFIER ON is set.

Why?

The setting of "SET QUOTED IDENTIFIER" is defined at creation time for stored procs and is always "ON" for tables. Source, BOL.

When a table is created, the QUOTED
IDENTIFIER option is always stored as
ON in the table's metadata even if the
option is set to OFF when the table is
created.

When a stored procedure is created,
the SET QUOTED_IDENTIFIER and SET
ANSI_NULLS settings are captured and
used for subsequent invocations of
that stored procedure.

The default for connections can be defined at the server level (sp_configure 'user options') or database level (ALTER DATABASE). For SSMS, it's under "Tools..Options.. Query Execution..SQL Server..ANSI". It's also the default for client libraries too (except DB-LIb).

Now, it you open an SSMS Query Window and start typing "CREATE PROC.." then it uses SSMS settings when you run the code.

And SET QUOTED IDENTIFIER can not be set at run time inside the stored proc. Show me the a reference before you disagree... From the MS BOL link above:

When executed inside a stored
procedure, the setting of SET
QUOTED_IDENTIFIER is not changed.

You have to work hard to run any code with this OFF... so the most likely fix is to ALTER or re-create the stored proc.

我很坚强 2024-08-02 23:27:14

我刚刚读了 Erland Sommarskog 的这篇文章,动态 SQL 的诅咒与祝福,它包括以下关于 SET QUOTED IDENTIFIER 设置的段落:

默认值
设置取决于上下文,但是
首选设置为 ON,并且必须
为 ON 以便使用 XQuery,已索引
计算列上的视图和索引。

您的存储过程是否使用 XQuery、索引视图或计算列上的索引?

I was just reading this article by Erland Sommarskog, The Curse and Blessings of Dynamic SQL, and it includes the following paragraph in regards to the SET QUOTED IDENTIFIER setting:

The default for this
setting depends on context, but the
preferred setting is ON, and it must
be ON in order to use XQuery, indexed
views and indexes on computed columns.

Does your stored procedure make use of XQuery, indexed views or indexes on computed columns at all?

尽揽少女心 2024-08-02 23:27:14

在 SQL Server 2005 中,SET QUOTED IDENTIFIER 默认情况下处于关闭状态,而不是打开状态(除非使用 ODBC 或 OLE 连接...请参阅 了解更多信息)。

您无需使用 SET QUOTED IDENTIFIER ON 创建表即可使用它。

您需要做的就是将 SET QUOTED IDENTIFIER ON 添加到 SP 的开头,以使其能够运行该过程(并确保如果您不想将其保留为打开状态,则必须将 SET QUOTED IDENTIFIER OFF 切换为回来了)。

编辑

我纠正了。 根据此 MSDN 页面,设置引用标识符默认情况下为 ON(除非与 DB-Library 应用程序连接)。

In SQL Server 2005, SET QUOTED IDENTIFIER is OFF by default, not ON (unless using an ODBC or OLE connection...see this for more information).

You do not need to create the table with SET QUOTED IDENTIFIER ON to use it.

All you need to do is add SET QUOTED IDENTIFIER ON to the beginning of your SP to enable it for the run of the procedure (and make sure that if you don't wish to leave it on, you have SET QUOTED IDENTIFIER OFF to switch it back).

EDIT

I stand corrected. According to this MSDN Page, SET QUOTED IDENTIFIER is ON by default (unless connection with a DB-Library application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文