Home > Software design >  I am getting Syntax error in sql (insert)
I am getting Syntax error in sql (insert)

Time:11-13

So, I am trying to add a data if a value in field does not exist. I am keep getting syntax error and not sure where I am getting it wrong.

INSERT INTO COMPANY_TABLE(company_name, company_phone, company_url)
VALUES ('test','010-4843-0000','www.company.com')
WHERE NOT EXISTS (SELECT * FROM COMPANY_TABLE WHERE company_name = 'test');

This is my code. I am using H2 database

CodePudding user response:

You're trying to combine a values table constructor with syntax of a select query

You can insert into a table using select:

INSERT INTO COMPANY_TABLE(company_name, company_phone, company_url)
SELECT 'test','010-4843-0000','www.company.com'
WHERE NOT EXISTS (SELECT * FROM COMPANY_TABLE WHERE company_name = 'test');
  • Related