我有一个 SQL 代码来更新列中的值。 我需要找到一个适用于 MS SQL 和 MySQL 的查询。 主要问题是我使用的列是 Default 和 Type,它们是 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 引号。如果这样做,您可以使用双引号来引用您的标识符。
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.
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/