JPQL CreateQuery with SYSDate comparison - jpa-2.1

I have a JPQL query which is giving me error as below and same query running fine when I used NativeSQLQuery, THe same JPQL code was running on JBoss 6 PERFECTLY WITH JAVAEE7 but did not work on PAYARA 5.192 with javaee8.
Exception Description: Syntax error parsing [SELECT weblook FROM WebLookup weblook WHERE weblook.lookupType=:looktype AND NVL(weblook.inactiveDate, SYSDATE + 1) > SYSDATE ORDER BY lookupDescription ]. The right expression is not a valid expression.
NativeSQL :
SELECT * FROM MW_WEB_LOOKUP mwWebLookUp WHERE mwWebLookUp.LOOKUP_TYPE=:LOOKUPTYPE
AND NVL(mwWebLookUp.INACTIVE_DATE , SYSDATE + 1) > SYSDATE ORDER BY mwWebLookUp.LOOKUP_DESCRIPTION

SYSDATE is provider specific, e.g. not part of the JPA standard. Standard would be CURRENT_DATE, CURRENT_TIME or CURRENT_TIMESTAMP.
Here are some examples that you could try:
How do I query "older than three minutes" in JPA?

Related

Query date range?

Trying to preform a date range query on a datasource example:
query.where = 'TransactionDate BETWEEN: StartDate AND EndDate';
This is what I get:
Unexpected input at ': StartDate AND EndDate'.
Error: Unexpected input at ': StartDate AND EndDate'. at datasources.
I was assuming this would work similarly to a MySQL query:
WHERE TransactionDate BETWEEN "2012-03-15" AND "2012-03-31";
In order to use real SQL query you need to go with Calculated SQL model. With query.where = ... you are setting App Maker's Query Builder expression that supports limited set of operations. I think your Query Builder expression will look similar to this:
TransactionDate >= :StartDate AND TransactionDate <= :EndDate

Create a table as, where 'date condition' in dynamic PL/SQL

I got assigned the following task.
Assume we have a table A structured with an id column and a date column.
Write a procedure in PL/SQL that: takes as parameters the table name (in our case A) and a date D, creates a backup table named A_bck containing only the records of A with dates < D and removes from the table A all the records inserted in A_bck.
Here there is my code.
Unluckily I get this error:
Error report -
ORA-00904: "MAY": invalid identifier
ORA-06512: at line 41
ORA-06512: at line 80
00904. 00000 - "%s: invalid identifier"
If I try to achieve the same result using a where condition on the id column instead that on the date one, I have no problems.
Where is the mistake? Am I implementing it completely in the wrong way?
The problem you have is that as you're executing dynamic sql you're query is built up as a string. Oracle does not know that the date you've given is actually a date, it is simply being treated as part of the string. To solve this you should be able to do the following:
my_query := 'CREATE TABLE ' || table_name_backup || ' AS (SELECT * FROM ' || table_name || ' WHERE table_date < to_date(''' || backup_date || '''))';
This should sort out your issue for you. As a side note, you will probably want to change your "table_exists" query, as table names are all stored in upper case, e.g.
SELECT COUNT(*) INTO table_exists FROM USER_TABLES WHERE TABLE_NAME = upper(my_table);
Edit: Further explanation following comment
To explain why you don't have the above problem when using integers, it is important to remember that using execute immediate simply executes the given string as an SQL query.
For example:
declare
x INTEGER := 1;
i integer;
my_query VARCHAR2(256);
begin
my_query := 'select 1 from dual where 1 = ' || x;
EXECUTE IMMEDIATE my_query INTO i;
end;
my_query in the above example would be executed as:
select 1 from dual where 1 = 1
which is perfectly valid sql. In your example however, you were ending up with something like this:
CREATE TABLE abaco_bck AS (SELECT * FROM abaco WHERE table_date < 27-MAY-17)
As it isn't wrapped in quotes, or explicitly converted to a date, the SQL engine is trying to subtract "MAY" from 27, but it doesn't know what "MAY" is.
One other thing to mention, is that for some operations you could use bind variables instead of quotes (although you can't for DDL) e.g.
declare
lToday DATE := SYSDATE;
i INTEGER;
my_query VARCHAR2(256);
begin
my_query := 'select 1 from dual where sysdate = :1';
EXECUTE IMMEDIATE my_query INTO i USING lToday;
end;

SQlite LIMIT and [ROW_NUMBER]

In SQL this command works ok:
Query
SELECT TOP 20 * FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY t0.ProductId) AS [ROW_NUMBER], *
FROM Product AS [t0]
) AS [t1]
WHERE [t1].[ROW_NUMBER] > 0 * 20;
Now I try the same with SQLite. I know that I must replace TOP with LIMIT, but don't know where to put it.
I always get something like
Error
SQLite error near "(": syntax error" or "SQLite error near "*": syntax error".
And I am not sure that the command [ROW_NUMBER] or ROW_NUMBER() works in SQlite.
Query
SELECT *,
(
SELECT COUNT(*)
FROM Product b
WHERE a.ProductId >= b.ProductId
) AS rnum
FROM Product a LIMIT 20;
Screen Shot
See the documentation:
SELECT *
FROM Product
LIMIT 20
OFFSET 0 -- optional
SQlite doesn't support TOP. That is sql-server syntax. You have to use limit 20 instead.

SQLite Syntax Error

I made a sql query, below. I am getting an exception which I have shown as well. Any help is appreciated.
QUERY:
INSERT OR REPLACE INTO CLAIMS (DATE ,TIME , ADDRESS , CITY,
STATE , POSTAL , PHFNAME ,PHLNAME ,PHEMAIL ,PHPHONE ,AGENCY ,POLICY ,VEHICLENAME,
YEAR ,MAKE ,MODEL ,PLATELICENSE ,LSTATE ,VIN ,DRIVERNAME,DRFNAME ,DRLNAME ,
DRPHONE ,DREMAIL ,DRLICENSE) VALUES("Wednesday, May 4, 2011",
"10:39:10 PM EDT", "400 Chatham","Pune", "Penn", "45223", "John",
"Richard","jsmith#newyahoo.com","+1-11111111111",
"Three Rivers Insurance","(null)", "(null)", "(null)",
"(null)", "(null)","(null)", "(null)", "(null)","(null)",
"(null)", "(null)","(null)", "(null)","(null)") WHERE DATE LIKE
'Wednesday, May 4,%' AND TIME = '10:39:10 PM EDT'
Error:
SQLiteManager: Likely SQL syntax error:
[ near "WHERE": syntax error ]
Exception Name: NS_ERROR_FAILURE
Exception Message: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [mozIStorageConnection.createStatement]
I faced the same issue for sync table for insert and update at a time and finally i use.
To use the insert or replace statement you need to use the coalesce function that required primary key column to put the where clause.
COALESCE((SELECT PRIMARY_KEY_COLUMN FROM TABLE_NAME WHERE UNIQUE_COLUMN = 'VALUE'),
(SELECT MAX(PRIMARY_KEY_COLUMN)+1 FROM TABLE_NAME))
it worked for me e.g
INSERT OR REPLACE INTO TBL_ADDRESSES
(ADDRESS_ID,ADDRESS1,ADDRESS2,ADDRESS3,ADDRESS_NAME,CITY,DB_ID)
VALUES (
COALESCE((SELECT ADDRESS_ID FROM TBL_ADDRESSES WHERE DB_ID = '111'),
(SELECT MAX(ADDRESS_ID)+1 FROM TBL_ADDRESSES)),
'IT 27','Pratap DSFSDSDDSDSF','test add ','IT 27','Jaipur','111') ;
INSERT OR REPLACE doesn't support WHERE clause, see theSQLite Insert doc. You can use a WHERE clause in UPDATE statements, see the SQLite Insert doc
Can help you if you can let us know what you wanted here ?

JDOQL Subquery count problems

I am having trouble with subquery counts with JDOQL (using DataNucleus). The following query
SELECT this.price
FROM com.mysema.query.jdo.test.domain.Product
WHERE (SELECT count(other)
FROM com.mysema.query.jdo.test.domain.Product other
WHERE other.price > this.price) > a1
PARAMETERS java.lang.Long a1
causes the Exception
javax.jdo.JDOUserException: Cannot perform operation ">" on SubqueryExpression "(SELECT COUNT("OTHER".PRODUCT_ID) FROM PRODUCT "OTHER" WHERE "OTHER".PRICE > THIS.PRICE)" and IntegerLiteral "?"
at org.datanucleus.jdo.NucleusJDOHelper.getJDOExceptionForNucleusException(NucleusJDOHelper.java:390)
at org.datanucleus.jdo.JDOQuery.executeWithArray(JDOQuery.java:321)
The following query works neither :
SELECT this.price
FROM com.mysema.query.jdo.test.domain.Product
WHERE !(SELECT other
FROM com.mysema.query.jdo.test.domain.Product other
WHERE other.price > this.price).isEmpty()
What is the proper way to make sure that a subquery result is not empty?
I got the problems fixed by upgrading from DataNucleus 2.0.4 to 2.1.2. The first query works.

Resources