WHERE NOT EXISTS in PostgreSQL gives syntax error - sql

When trying to use the WHERE NOT EXISTS clause to prevent adding a row with a duplicate value in the column age, I get the error syntax error at or near "WHERE".
Why did it throw a syntax error? I'm using Postgresql 9.1.
SQL
INSERT INTO live.users ("website", "age")
values ('abc', '123')
WHERE NOT EXISTS (SELECT age FROM live.users WHERE age = 123);
Error
ERROR: syntax error at or near "WHERE"
LINE 6: WHERE NOT EXISTS (SELECT age FROM live.users W...

Do instead:
INSERT INTO live.users ("website", "age")
SELECT 'abc', 123
WHERE NOT EXISTS (SELECT age FROM live.users WHERE age = 123);

INSERT INTO live.users ("website", "age")
select 'abc', '123'
WHERE NOT EXISTS (SELECT age FROM live.users WHERE age = 123);

I encountered some issues in using WHERE field NOT EXISTS in PLPGSQL. Instead what worked well was WHERE field NOT IN, I received no function errors after using that.

I see you asked for v9.1 but it's been 4 yrs since and now, starting from PostgreSQL v9.5 - INSERT gives you ON CONFLICT … DO NOTHING option:
INSERT INTO live.users("website", "age") VALUES('abc', '123') ON CONFLICT ("age") DO NOTHING
Worth noting this requires respective constraint set up on the target table - but in most cases, I imagine you would have it anyway. Otherwise you'll get:
ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification

Related

I am getting Syntax error in sql (insert)

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
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');

Update multiple values from temporary table

I'm very puzzled because when I'm running this query on SQLite.
On MacOS Mojave SQLITE, I'm getting a syntax error on the on "FROM". There is no more detail.
This does work on Postgres.
Am I reading the SQLite documentation the wrong way? https://sqlite.org/lang_update.html
Here's the query:
BEGIN;
-- Statement 1
CREATE TEMP TABLE tempEdits (identifier text, serverEditTime double precision);
-- Statement 2
INSERT INTO tempEdits (identifier, serverEditTime)
VALUES
('uuid1', 1.5),
('uuid2', 2.2),
('uuid3', 3.3);
-- Statement 3
UPDATE
"pEdits"
SET
"serverEditTime" = t.serverEditTime
FROM
"pEdits" AS e JOIN tempEdits AS t ON e.identifier = t.identifier
WHERE
e.identifier = t.identifier;
END;
Setup query:
CREATE TABLE "pEdits" (identifier text, serverEditTime double precision);
INSERT INTO (identifier)
VALUES
('uuid1'),
('uuid2'),
('uuid3');
SQLite does not support joins in the UPDATE statement.
Instead you should use a correlated subquery:
UPDATE pEdits
SET serverEditTime = (
SELECT t.serverEditTime
FROM tempEdits AS t
WHERE t.identifier = pEdits.identifier
);
See the demo.
Edit: Starting from version 3.33.0+ (2020-08-14), SQLite supports the Postgresql-like FROM clause. See https://www.sqlite.org/lang_update.html#upfrom
The logic you want is:
UPDATE "pEdits"
SET "serverEditTime" = t.serverEditTime
FROM tempEdits t
WHERE "pEdits".identifier = t.identifier;
In other words, the table being updated should not be repeated in the FROM clause -- well, unless your intention is a self-join.

Missing select keyword in Oracle SQL

I am getting this error
SQL Error: ORA-00928: missing SELECT keyword
00928. 00000 - "missing SELECT keyword"`
when I am trying to insert like this
create table certf
(
certificate_id integer primary key,
certificate_name varchar(100) not null,
certificate_content varchar(300) not null
);
insert into certf (&certificate_id, &certificate_name, &certificate_content);
You are missing values actually:
insert into certf (certificate_id, certificate_name, certificate_content)
values (&certificate_id, &certificate_name, &certificate_content);
Notice that I also added the column list to the insert. This is a best practice.
If those are supposed to be values you're providing as substitution values, then you're missing the values keyword:
insert into certf values (&certificate_id,&certificate_name,&certificate_content);
But you need the string values to be in quotes:
insert into certf values (&certificate_id,'&certificate_name','&certificate_content');
and you should supply the column names too:
insert into certf (certificate_id,certificate_name,certificate_content)
values (&certificate_id,'&certificate_name','&certificate_content');
With you current code the parser is seeing that first list of - possible, but actually invalid in this case - identifiers, i.e column names; because it hasn't seen that values keyword yet. It's treated as something like:
insert into certf (42,some_name,some_content);
And having done that, and when it still doesn't see a values keyword or values list, it's expecting this to be an insert ... select construct instead. You could do it that way:
insert into certf (certificate_id,certificate_name,certificate_content)
select &certificate_id,'&certificate_name','&certificate_content' from dual;
But you aren't doing that. So it doesn't see the select either, and it throws the error you see.

ERROR: syntax error at or near "SELECT"

I am really new to postgres. The question looks very simple but I just cant see where I got wrong.
I a table created as follows:
CREATE TABLE IF NOT EXISTS t(
tn VARCHAR(30) NOT NULL,
PRIMARY KEY(tn)
);
I want to insert an instance if the instance does not exist. Here is my code:
INSERT INTO t (tn)
VALUES
(SELECT 'q' WHERE NOT EXISTS (SELECT * FROM t WHERE tn = 'q')) ;
And the psql console keeps giving me the error
ERROR: syntax error at or near "SELECT"
I have checked every piece of code individually, for instance both
SELECT 'q' WHERE NOT EXISTS (SELECT * FROM t WHERE tn = 'q');
and
INSERT INTO t (tn) VALUES ('p');
run without error. But error occurs when I put them together.
Does anyone know where I got wrong..?
Lose VALUES and the brackets...
INSERT INTO t (tn)
SELECT 'q' WHERE NOT EXISTS (SELECT * FROM t WHERE tn = 'q');

Inserting unique value into database PGSQL/MSSQL

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.