Inserting unique value into database PGSQL/MSSQL - sql

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.

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

How to insert table in Databricks using magic SQL operator

I have create the following SQL table in databricks (using the magic %sql) as follows:
%sql
CREATE TABLE mytable (
id INT
,name STRING
,met_area_name STRING
,state STRING
,type STRING
) USING CSV
I am now trying insert data into the table using the following command:
%sql
INSERT INTO TABLE mytable VALUES (id,name,type)
SELECT DISTINCT criteria1, criteria2, 'b'
FROM tablex
WHERE somecriteria1 = 0
ORDER BY somecriteria2;
However, I'm getting the following error:
Error in SQL statement: ParseException:
mismatched input 'FROM' expecting <EOF>(line 2, pos 2)
== SQL ==
INSERT INTO TABLE mytable VALUES (id,name,type)
FROM tablex
--^^^
WHERE somecriteria1 = 0
ORDER BY somecriteria2
I'm sure there is something very obvious that I'm missing, but I can't see it.
Any assistance much appreciated.
Cheers

hsql gives exception executing this statement

I am trying to run my integration test for my Spring Cloud Task Batch Application using in memory hsql db and it gives me the below error executing this db statement. This statement executes fine in sqlserver db. Is there some syntax change I need to do? Thanks!
Table Create Query
CREATE TABLE TASK_SEQ (
ID BIGINT NOT NULL,
UNIQUE_KEY CHAR(1) NOT NULL,
constraint UNIQUE_KEY_UN unique (UNIQUE_KEY)
);
Insert Query
INSERT INTO TASK_SEQ (ID, UNIQUE_KEY) select * from (select 0 as ID, '0' as UNIQUE_KEY) as tmp;
Error Stacktrace
Caused by:
org.springframework.jdbc.datasource.init.ScriptStatementFailedException:
Failed to execute SQL script statement #1 of class path resource
[schema-DML.sql]: INSERT INTO TASK_SEQ (ID, UNIQUE_KEY) select * from
(select 0 as ID, '0' as UNIQUE_KEY) as tmp; nested exception is
java.sql.SQLSyntaxErrorException: unexpected token: ) at
org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:491)
You need to enable SQL Server compatibility in HSQLDB. See the Guide:
http://hsqldb.org/doc/2.0/guide/compatibility-chapt.html#coc_compatibility_mssql
Your statement looks unnecessarily complex. Something like below should work in most databases.
INSERT INTO TASK_SEQ (ID, UNIQUE_KEY) VALUES (0, '0')

Insert multiple records in oracle [duplicate]

This question already has answers here:
Best way to do multi-row insert in Oracle?
(9 answers)
Closed 8 months ago.
I am using oracle sql developer to insert rows in my database.
While this request is working :
INSERT INTO TABLE ( USERID, USERNAME) VALUES (1,"ok1")
The second one (when I am trying to insert multiple rows)is not working:
INSERT INTO TABLE ( USERID, USERNAME) VALUES (1,"ok1"),(2,"ok2")
I am getting this error :
Erreur SQL : ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
You could use INSERT ALL statement. For example:
INSERT ALL
INTO mytable (column1, column2, column3) VALUES ('val1.1', 'val1.2', 'val1.3')
INTO mytable (column1, column2, column3) VALUES ('val2.1', 'val2.2', 'val2.3')
INTO mytable (column1, column2, column3) VALUES ('val3.1', 'val3.2', 'val3.3')
SELECT * FROM dual;
Oracle does not support multi-row inserts. You need to write one insert per row:
INSERT INTO TABLE ( USERID, USERNAME) VALUES (1,'ok1');
INSERT INTO TABLE ( USERID, USERNAME) VALUES (2,'ok2');
Additionally: string literals need to be enclosed in single quotes in SQL. Double quotes are for identifiers. "ok1" is a column name, 'ok1' is a string constant.
See this recent post for some other ways to enter test data as well:
Insert same data multiple times
If you are not worried about SQL injection then run the following :
BEGIN
INSERT INTO TABLE ( USERID, USERNAME) VALUES (1,'ok1');
INSERT INTO TABLE ( USERID, USERNAME) VALUES (2,'ok2');
END;

WHERE NOT EXISTS in PostgreSQL gives syntax error

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