向数据库PGSQL/MSSQL插入唯一值

时间:2021-08-12 11:10:08

I'm trying to insert unique records in a MSSQL and Postgresql DB using insert into where not exists. But I am getting a incorrect syntax error as seen below. What am I doing wrong?

我试图在MSSQL和Postgresql DB中使用insert插入来插入不存在的唯一记录。但是我得到了一个错误的语法错误,如下所示。我做错了什么?

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

Error: [Macromedia][SQLServer JDBC Driver][SQLServer]Incorrect syntax near the keyword 'WHERE'.

错误:[Macromedia][SQLServer驱动程序][SQLServer]在关键字'WHERE'附近语法不正确。

1 个解决方案

#1


5  

Try this:

试试这个:

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

sql服务器sql小提琴

postgresql sql fiddle

postgresql sql小提琴

WHERE is a filter for results which are not typically pertinent to INSERT operations.

其中的筛选器用于通常与插入操作无关的结果。

#1


5  

Try this:

试试这个:

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

sql服务器sql小提琴

postgresql sql fiddle

postgresql sql小提琴

WHERE is a filter for results which are not typically pertinent to INSERT operations.

其中的筛选器用于通常与插入操作无关的结果。