HQL Query starting with REPLACE - hql

I have a query starting with REPLACE. When I use it directly in MySQL console everything is fine, so query is ok. However, when I put it in my code like this:
#Query("REPLACE INTO WeekAggregate...
There is an error:
web - 2016-10-05 10:35:44,297 [localhost-startStop-1] ERROR o.h.hql.internal.ast.ErrorCounter - line 1:1: unexpected token: REPLACE
antlr.NoViableAltException: unexpected token: REPLACE
How could I fix this? Is REPLACE not supported in HQL?

HQL is trasversal to underlying DBMS. So some functions you're using in your SQL can't be interpretated by HQL.
You have two ways:
change your HQL query (in this case you can rewrite your query with an DELETE/INSERT statement)
you can write your query in SQL and you can use a method createSqlQuery so the interpreter, runs the query as SQL native query.

I changed my query to native SQL query by using nativeQuery = true flag.

Related

Oracle ORA-00933: SQL command not properly ended

I try to use golang and query data from Oracle. My SQL query is:
SELECT * FROM TABLE1 OFFSET 10 ROWS;
But it gives an error:
EXTRA *errors.withStack=dpiStmt_execute: ORA-00933: SQL command not properly ended
My SQL query works fine when I query in SQL*Plus, but errors when I use golang.
I'd try running the query without the terminating semicolon, as Alex Poole pointed out. A lot of Oracle client libraries (i.e. cx_Oracle in python, ADO.NET Oracle Libraries) do complain if you try to execute a query ending with the semicolon (which is perfectly legal in SQL/Plus)
If the offset is not specified, it is assumed that it is 0 (zero). So, remove that clause (as it does nothing in your case), i.e.
select * from table1
and use that query in golang.
I am pretty sure that just your closing semicolon is to much. The semicolon is a character to separate several SQL statements or to close a pl/sql block. So when you write it at the end of a SQL statement the parser doesn't know how to handle it, cause he only awaits a single SQL statement.

Spring data: CONCAT are removing commas when used with TRANSLATE

I'm trying to perform the following query:
SELECT p FROM Pessoa p WHERE TRANSLATE(UPPER(p.nome), 'ÂÁÀÄÃÊÉÈËÎÍÌÏÔÓÒÖÕÛÚÙÜÇ', 'AAAAAEEEEIIIIOOOOOUUUUC') LIKE CONCAT('%',TRANSLATE(UPPER(?1), 'ÂÁÀÄÃÊÉÈËÎÍÌÏÔÓÒÖÕÛÚÙÜÇ', 'AAAAAEEEEIIIIOOOOOUUUUC'),'%')
But i realize that hibernate is generating the following final SQL:
... where TRANSLATE(upper(pessoa0_.ds_nome), 'ÂÁÀÄÃÊÉÈËÎÍÌÏÔÓÒÖÕÛÚÙÜÇ', 'AAAAAEEEEIIIIOOOOOUUUUC') like ('%'||TRANSLATE(upper(?)||'ÂÁÀÄÃÊÉÈËÎÍÌÏÔÓÒÖÕÛÚÙÜÇ'||'AAAAAEEEEIIIIOOOOOUUUUC')||'%')...
Note that the commas inside the TRANSLATE function were replaced by || which leads to an org.postgresql.util.PSQLException: ERROR: function translate(text) does not exist
What am I doing wrong?
Spring: 4.3.4.RELEASE
Spring data: spring-data-jpa:1.10.5.RELEASE
Hibernate is not simply replacing commas. It is converting a JPQL (or possibly HQL) query int SQL. The double pipes || are a common SQL syntax for the JPQL CONCAT function.
If it doesn't work for your Postgres DB you are probably using the wrong dialect. Configure Hibernate to use the correct Dialect for your database.
See for example this article for how to do that.

Running multi-statement query using JDBC on DB2 windows

I am trying to run multi-statement queries using JDBC on DB2 10.1 Windows but it fails with a syntax error. Following is the query-
SELECT * FROM schemaname.tablename;
Exception in thread "main" com.ibm.db2.jcc.am.SqlSyntaxErrorException: An unexpected token "" was found following "". Expected tokens may include: "schemaname.tablename".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.13.127
I understand that this comes due to the semi-colon at the end of the query which is not understand by the database. How can I set the query separator as semicolon so get through this.
With JDBC you execute one statement at a time; by the looks of it DB2 doesn't support it. Some drivers/databases have a way around this, but it is non-standard.
So instead of trying to execute two statements in one go, you will need to execute them one after the other.

Error running SQL queries with Liquibase

I'm using Liquibase to create tables in DB2. I have a simple example changelog that tries to drop and then create a table.
The SQL statements work fine via my DbVisualizer tool (which uses the same JDBC driver as Liquibase) and also works fine when submitted via the db2 command line tool.
Here's the Liquibase input file:
--changeset dank:1 runAlways=true failOnError:false
DROP TABLE AAA_SCHEMA.FOO
--changeset dank:2 runAlways=true
CREATE TABLE AAA_SCHEMA.FOO ( MYID INTEGER NOT NULL )
Here's the error message I get:
Caused by: com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error:
SQLCODE=-104, SQLSTATE=42601, SQLERRMC=DROP TABLE AAA_SCHEMA.FOO;
;, DRIVER=4.18.60
The IBM error code -104 is about syntax problems. Based on looking at the error message my guess is that it has something to do with the end of line character ";". But I've tried the query with and without the semi-colon. The semi-colon is accepted by IBM's own db2 too, so it seems like a valid choice.
Any help in figuring out the cause of this error is much appreciated.
The problem was me forgetting to start my native sql file with this required line:
--liquibase formatted sql
Doh!

ORA-00907 when quering from my Java application but works fine in SQL Developer

My query that I put into a prepared statement is:
select *
from ( select seq, audit_ts, message_type
from log2
where 1 = 1
and message_type in ('SOURCE', 'DEST')
order by seq desc )
where ROWNUM <= ?
When I run the query in my application, I get:
java.sql.SQLSyntaxErrorException: ORA-00907: missing right parenthesis
EDIT: Here is the java executing the query. I am trying to return a set of search results, so the prefix contains the SELECT statement and then I can have any number of suffixes (in this excerpt "AUDIT_LOG_SEARCH2") which are the parameterized WHERE clauses based on the user search:
StringBuffer query = new StringBuffer(300);
query.append(dbAdapter.getQuery("AUDIT_LOG_ENTRY_PREFIX"));
query.append(dbAdapter.getQuery("AUDIT_LOG_SEARCH2"));
// Insert parameters to complete the sql prepared statement
PreparedStatement ps = _dbConn.prepareStatement(query.toString());
ResultSet rs = ps.executeQuery();
But the query runs fine when I run it separately in SQL Developer. The query was originally created for Postgres, then updated for Oracle. Any tips?
You need to set the variables into the preparedStatement before executing.
PreparedStatement ps = _dbConn.prepareStatement(query.toString());
ps.setInt(1, 10);
Please post what query.toString() gives you if that doesn't work. Not query, but query.toString()
What are you doing in your:
// Insert parameters to complete the sql prepared statement
Are you using correctly the methods ps.setString... or whatever? Or are you just replacing the question marks? the second might be corrupting your query.
Based on #AlexPoole and #EdGibbs comments, I decided to add a bunch more debug statements. It turns out the method was being recursively called with a different sql "suffix" later on in the program if certain conditions were met. The suffix was not updated with the necessary parenthesis for the new ROWNUM wrapping the statement. So although the ORA-00907 can be thrown for many different formatting problems, it was in fact a right parenthesis that was causing me problems :P
The prefix and suffix seems like a weird pattern in this code base for creating sql queries. I'm thinking of getting rid of this and refactoring so queries don't have to be built like that. Any advice??
So for anyone else who runs into this Oracle error, I would suggest logging the sql statement you are generating and play around with it in SQL Developer. If it works in there, but not in your application, your code is probably doing something funky :P

Resources