hsql gives exception executing this statement - sql

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

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

How to save query result when using SQL in R?

If just SQL, we can use the following code to save a query result in a temporary table, so that we can use the result later.
CREATE TABLE #TEMPTABLE
(
Column1 type1,
Column2 type2,
Column3 type3
)
INSERT INTO #TEMPTABLE
SELECT ...
SELECT *
FROM #TEMPTABLE ...
This answer is from How to save select query results within temporary table?
However, I am using R to connect HANA. I need to use SQL query in R to select data from HANA. I need a tempory table for my query result. My code is like this:
sqlQuery(ch,paste('
CREATE TABLE #myTemp(
"/BIC/ZSALE_OFF" INT
)
INSERT INTO #myTemp
SELECT
"/BIC/ZSALE_OFF"
FROM
"SAPB1D"."/BIC/AZ_RT_A212"
'))
I have got the following error information:
[1] "42000 257 [SAP AG][LIBODBCHDB DLL][HDBODBC] Syntax error or access violation;257 sql syntax error: incorrect syntax near \"INSERT\": line 5 col 19 (at pos 124)"
[2] "[RODBC] ERROR: Could not SQLExecDirect '\n CREATE TABLE #myTemp(\n \"/BIC/ZSALE_OFF\" INT\n )\n INSERT INTO #myTemp\n SELECT\n \"/BIC/ZSALE_OFF\"\n FROM\n \"SAPB1D\".\"/BIC/AZ_RT_A212\"\n
Without the temperory result part, the code for just query is correct:
sqlQuery(ch,paste('
SELECT
"/BIC/ZSALE_OFF"
FROM
"SAPB1D"."/BIC/AZ_RT_A212"
'))
I am not sure if the grammar is correct, or there is something else I do not understand.

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.