문제

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?

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'.

도움이 되었습니까?

해결책

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

postgresql sql fiddle

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top