gpt4 book ai didi

mysql - 列名默认为 SQL 更新语句

转载 作者:行者123 更新时间:2023-11-29 01:57:52 25 4
gpt4 key购买 nike

我有一个 SQL 代码来更新列中的值。我需要找到一个适用于 MS SQL 和 MySQL 的查询。主要问题是我使用的列是默认和类型,它们是在 SQL 中保存的名称,因此它不适用于正常的更新语句。

我找到了以下解决方案,但我想对两者进行一个查询 -

--Clearing Data logs Defualt MS SQL
UPDATE queries
SET [Default] = 0
FROM queries
WHERE [Type] = 4

--Clearing Data logs Defualt MySQL
UPDATE queries q
SET q.Default = 0
WHERE q.TYPE = 4

非常感谢您的帮助!

最佳答案

您必须为两个服务器启用 ANSI 引号。如果这样做,您可以使用双引号来引用您的标识符。

For MySQL :

SET sql_mode = 'ANSI_QUOTES'

Treat “"” as an identifier quote character (like the “” quote
character) and not as a string quote character. You can still use “
” to quote identifiers with this mode enabled. With ANSI_QUOTES enabled, you cannot use double quotation marks to quote literal strings, because it is interpreted as an identifier.

For MS SQL Server .

SET QUOTED_IDENTIFIER ON

Causes SQL Server to follow the ISO rules regarding quotation mark delimiting identifiers and literal strings. Identifiers delimited by double quotation marks can be either Transact-SQL reserved keywords or can contain characters not generally allowed by the Transact-SQL syntax rules for identifiers.

现在您可以编写同时适用于 MS SQL Server 和 MySQL 的单个查询:

UPDATE queries q
SET q."Default" = 0
WHERE q."TYPE" = 4

在我忘记之前:解决此类问题的最佳方法是避免使用保留字作为标识符,请参阅 solution 1 .您必须避免所有相关领域的保留字(T-SQL 和 MySQL 的 sql 方言)。

关于mysql - 列名默认为 SQL 更新语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23455333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com