mysql - 以默认值作为列名的 SQL 更新语句

标签 mysql sql sql-server

我有一个 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 引号。如果这样做,您可以使用双引号来引用您的标识符。

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/

相关文章:

php - Codeigniter 数据库错误编号 1048 值显示 NULL,即使它们不是 NULL

mysql - 使用 openssl 加密字段过滤 MySQL 表

c# - 将 .NET SqlType 与 MySQL 结合使用

mysql - 如何借助mysql中的sql事务id查找进程id

php - 分解字符串然后从数据库中获取

mysql - 无法将 DATETIME 列添加到 MySQL 表

php - 为什么我的左连接在我的 php mysql 数据库中不起作用?

sql - 如何在 SQL Server 上为 DDL 启用事务

sql - 将 varchar 值转换为数据类型 int 时转换失败 - 但我没有转换任何内容

sql - 替换 SQL 查询中的多个字符串