【问题标题】:Inserting unique value into database PGSQL/MSSQL将唯一值插入数据库 PGSQL/MSSQL
【发布时间】:2013-01-14 23:31:49
【问题描述】:

我正在尝试使用 insert into where not exists 在 MSSQL 和 Postgresql DB 中插入唯一记录。但是我收到了一个不正确的语法错误,如下所示。我究竟做错了什么?

INSERT INTO settings (id, title, description)
VALUES  (1, 'imageHeight', 'Image Height')
WHERE NOT EXISTS (Select * from settings where id = 1);

错误: [Macromedia][SQLServer JDBC Driver][SQLServer]关键字“WHERE”附近的语法不正确。

【问题讨论】:

  • 我不能代表 MSSQL,但在 Postgres 上,INSERT 语句没有 WHERE 子句。

标签: sql sql-server postgresql insert-into not-exists


【解决方案1】:

试试这个:

INSERT INTO settings (id, title, description)
    SELECT 1, 'imageHeight', 'Image Height'
        WHERE NOT EXISTS (SELECT 1 FROM settings WHERE id = 1);

sql server sql fiddle

postgresql sql fiddle

WHERE 是一个过滤器,用于过滤通常与INSERT 操作无关的结果。

【讨论】:

    猜你喜欢
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多