Using BigDecimal field in #Query and populating it with #Param field is throwing Sql Error - spring-data-jpa

This following is the query i am using for setting BigDecimal value in Query but failing as error in SQL Syntax
#Query(value="Select f.id,s.student_id,f.feesPaid,f.fees_pending,f.paid_datetime from Fees f inner join Student s where f.feesPaid > :amt")
List<Fees> findFirst3ByFeesPaidGreaterThan( #Param(value = "amt") BigDecimal amt);
the following is the error
Hibernate: select fees0_.id as col_0_0_, student1_.student_id as col_1_0_, fees0_.fees_paid as col_2_0_, fees0_.fees_pending as col_3_0_, fees0_.paid_datetime as col_4_0_ from fees fees0_ inner join student student1_ on where fees0_.fees_paid>?
2019-05-07 20:06:16.779 WARN 21752 --- [nio-8082-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1064, SQLState: 42000
2019-05-07 20:06:16.779 ERROR 21752 --- [nio-8082-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where fees0_.fees_paid>500' at line 1
I am able to use the query method name but i wanted to do it using Query as mentioned above.

Related

Database-migration ms sql to postgresql

I am getting a syntax error when converting between SQL Server to PostgreSQL. Any thoughts?
IF (var_port_with_bmrk_total_mv != 0 AND var_bmrk_info IS NOT NULL) THEN
BEGIN
insert into t$tmp_diff
select #asof_dt asof_dt,#choiceID choiceID ,p.input_array_type ,p.group_order, CONVERT(DECIMAL(32,10),p.port_value/#var_port_total_mv) port_value,convert(decimal(32,10), isnull(bmrk_value/#port_with_bmrk_total_mv,0)) bmrk_value
from t$tmp_port_sum p, t$tmp_bmrk_sum b
where p.input_array_type=b.input_array_type and p.group_order = b.group_order
END;
ELSE
Original before conversion
insert into #tmp_other_diff
select #asof_dt asof_dt,#choiceID choiceID , b.input_array_type,b.grouping,convert(decimal(32,10),0) port_value, (bmrk_value/#port_with_bmrk_total_mv) bmrk_value
from #tmp_bmrk_other_sum b
where b.key_value not in ( select p.key_value from #tmp_port_other_sum p)
Error message:
Error occurred during SQL query execution
Reason:
SQL Error [42601]: ERROR: syntax error at or near ","
Position: 9030
the relevant comma being:
CONVERT(DECIMAL(32,10),p.port_value
There is no convert() function in Postgres. Use the SQL standard cast or the Postgres extension ::data type. In this case:
...., cast(0 as decimal(30,10)) port_value, ....
OR
...., 0::decimal(30,10) port_value, ...
Note: No comma after the expression. In the original port_value is the column alias. You need to keep it that way.

Unable To Assign Query Result To A Variable In PostgreSQL

I have been struggling to assign the query result to a temp variable in PostgreSQL and I just can't get it to work. Can anyone guide please?
I tried with the both the below queries and I get the error shown below. I then tried with temp table. But it still fails during the assignment to the temp variable.
set var.ITEM_ID =(select t.item_id from api_item t inner join api_identifier i on i.item_id=t.item_id where i.value='99999');
set var.ITEM_ID = select t.item_id from api_item t inner join api_identifier i on i.item_id=t.item_id where i.value='99999';
The first statement throws the error :-
[2020-03-03 08:10:50] [42601] ERROR: syntax error at or near "("
[2020-03-03 08:10:50] Position: 23
Second one throws the below error:-
[2020-03-03 08:10:55] [42601] ERROR: syntax error at or near "select"
[2020-03-03 08:10:55] Position: 24

Why is my query failing with SQLCODE=-204?

I'm writing some SQL for a report (this is my first time actually writing SQL so don't slam my syntax too much, lol) and the subquery seems to be failing. I'm not sure why because the report compiles just fine and I can get it into Jaspersoft Studio before it errors out when trying to preview it. Am I even doing the subquery correctly?
Thanks in advance.
SELECT
PERSON.LAST_NAME,
ACCOUNT.ACCOUNT_NUMBER,
SHARE_ESCROW.ID,
LOAN.PAYMENT_DUE_DATE,
LN_IMPOUND_ANALYSIS.CUSHION_AMOUNT,
LN_IMPOUND_ANALYSIS.NEXT_P_AND_I_AMOUNT,
LOAN.IMPOUND_AMOUNT,
LN_IMPOUND_ANALYSIS.NEXT_IMPOUND_AMOUNT,
SHARE_ESCROW.BALANCE,
SHARE_ESCROW.DESCRIPTION,
SHARE_ESCROW.CLOSE_DATE,
i.LTV,
CASE LOAN.PAYMENT_METHOD
WHEN 'T' THEN 'Automated Transfer'
WHEN 'A' THEN 'ACH'
WHEN 'C' THEN 'Cash'
END AS PAYMENT_METHOD
FROM
CORE.ACCOUNT AS ACCOUNT INNER JOIN CORE.LOAN AS LOAN ON ACCOUNT.SERIAL = LOAN.PARENT_SERIAL INNER JOIN
CORE.LN_IMPOUND_ANALYSIS AS LN_IMPOUND_ANALYSIS ON LOAN.SERIAL = LN_IMPOUND_ANALYSIS.PARENT_SERIAL INNER JOIN
CORE.SHARE AS SHARE_ESCROW ON ACCOUNT.SERIAL = SHARE_ESCROW.PARENT_SERIAL INNER JOIN
CORE.PERSON AS PERSON ON PERSON.SERIAL = ACCOUNT.PRIMARY_PERSON_SERIAL,
(SELECT
CASE COLLATERAL.AMOUNT
WHEN 0 THEN 0
WHEN NULL THEN 0
ELSE LOAN.BALANCE / COLLATERAL.AMOUNT
END AS LTV
FROM
CORE.COLLATERAL AS COLLATERAL INNER JOIN LOAN ON LOAN.SERIAL = COLLATERAL.PARENT_SERIAL
) i
WHERE
SHARE_ESCROW.DESCRIPTION = 'Escrow Share' AND
SHARE_ESCROW.CLOSE_DATE IS NULL AND
i.LTV != 0
Edit:
Here's the error:
net.sf.jasperreports.engine.JRException: net.sf.jasperreports.engine.JRRuntimeException: net.sf.jasperreports.engine.JRException: Error executing SQL statement for : Escrow32Analysis324532WIP_TableDataset_1579697577915_289752
at com.jaspersoft.studio.editor.preview.view.control.ReportControler.fillReport(ReportControler.java:466)
at com.jaspersoft.studio.editor.preview.view.control.ReportControler.access$18(ReportControler.java:441)
at com.jaspersoft.studio.editor.preview.view.control.ReportControler$4.run(ReportControler.java:333)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
Caused by: net.sf.jasperreports.engine.JRRuntimeException: net.sf.jasperreports.engine.JRException: Error executing SQL statement for : Escrow32Analysis324532WIP_TableDataset_1579697577915_289752
at net.sf.jasperreports.engine.fill.JRFillSubreport.prepare(JRFillSubreport.java:809)
at net.sf.jasperreports.components.table.fill.FillTableSubreport.prepareSubreport(FillTableSubreport.java:156)
at net.sf.jasperreports.components.table.fill.FillTable.prepare(FillTable.java:400)
at net.sf.jasperreports.engine.fill.JRFillComponentElement.prepare(JRFillComponentElement.java:151)
at net.sf.jasperreports.engine.fill.JRFillElementContainer.prepareElements(JRFillElementContainer.java:332)
at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:384)
at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:358)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillSummaryNoLastFooterSamePage(JRVerticalFiller.java:1102)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillSummary(JRVerticalFiller.java:1065)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportEnd(JRVerticalFiller.java:329)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:159)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:963)
at net.sf.jasperreports.engine.fill.BaseFillHandle$ReportFiller.run(BaseFillHandle.java:120)
at java.lang.Thread.run(Unknown Source)
Caused by: net.sf.jasperreports.engine.JRException: Error executing SQL statement for : Escrow32Analysis324532WIP_TableDataset_1579697577915_289752
at net.sf.jasperreports.engine.query.JRJdbcQueryExecuter.createDatasource(JRJdbcQueryExecuter.java:240)
at net.sf.jasperreports.engine.fill.JRFillDataset.createQueryDatasource(JRFillDataset.java:1114)
at net.sf.jasperreports.engine.fill.JRFillDataset.initDatasource(JRFillDataset.java:691)
at net.sf.jasperreports.engine.fill.JRBaseFiller.setParameters(JRBaseFiller.java:1314)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:931)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:873)
at net.sf.jasperreports.engine.fill.JRFillSubreport.fillSubreport(JRFillSubreport.java:665)
at net.sf.jasperreports.engine.fill.JRSubreportRunnable.run(JRSubreportRunnable.java:59)
at net.sf.jasperreports.engine.fill.AbstractThreadSubreportRunner.run(AbstractThreadSubreportRunner.java:203)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
... 1 more
Caused by: com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=CMURPHY.LOAN, DRIVER=4.13.127
at com.ibm.db2.jcc.am.id.a(id.java:677)
at com.ibm.db2.jcc.am.id.a(id.java:60)
at com.ibm.db2.jcc.am.id.a(id.java:127)
at com.ibm.db2.jcc.am.no.c(no.java:2653)
at com.ibm.db2.jcc.am.no.d(no.java:2641)
at com.ibm.db2.jcc.am.no.a(no.java:2090)
at com.ibm.db2.jcc.am.oo.a(oo.java:7671)
at com.ibm.db2.jcc.t4.cb.h(cb.java:141)
at com.ibm.db2.jcc.t4.cb.b(cb.java:41)
at com.ibm.db2.jcc.t4.q.a(q.java:32)
at com.ibm.db2.jcc.t4.sb.i(sb.java:135)
at com.ibm.db2.jcc.am.no.ib(no.java:2059)
at com.ibm.db2.jcc.am.oo.sc(oo.java:3559)
at com.ibm.db2.jcc.am.oo.b(oo.java:4348)
at com.ibm.db2.jcc.am.oo.fc(oo.java:743)
at com.ibm.db2.jcc.am.oo.executeQuery(oo.java:713)
at net.sf.jasperreports.engine.query.JRJdbcQueryExecuter.createDatasource(JRJdbcQueryExecuter.java:233)
... 11 more
The message you see in the log:
DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=CMURPHY.LOAN
means that the object (in your case a table) LOAN in the schema CMURPHY cannot be found (SQLCODE -204). As others have mentioned in comments, the only table reference in your query that is missing the explicit schema is this:
FROM
CORE.COLLATERAL AS COLLATERAL INNER JOIN LOAN ON LOAN.SERIAL ...
---------------------------------------------^^^^
so the database server by default looks for that table in the default schema, the one matching your user ID (CMURPHY). Apparently it's not there.
Add the explicit schema name to the table reference:
FROM
CORE.COLLATERAL AS COLLATERAL INNER JOIN CORE.LOAN AS LOAN ON LOAN.SERIAL ...

PostgreSQL: Why does this join query throw a syntax error

I don't understand, why the following query produced an error. Whereas in other dbms (eg mysql) this sort of query is absolutely legal.
ERROR: Syntax error at the end of input
LINE 1: SELECT * FROM "addr_country" AS c JOIN "addr_state" AS s
^
SQL state: 42601
Character: 59
Adding an on-clause to the query makes it working:
SELECT * FROM "addr_country" AS c JOIN "addr_state" AS s
ON c."id" = s."country" AND s.id = 10
Due to this answer, MySQL makes a cross join out of a join w/o conditions

db2 cast problem

[SQL]2010/12/07 20:18:32:184 : 0.0010 [update REG_COMP_DEF set OrderNo = Cast(Cast(SUBSTR(orderno,1,10) as numeric(10,0))+10 as varchar(10))||NVL(SUBSTR(orderno,10+1,length(orderno)-10),'') where length(OrderNo)>10 and OrderNo>='3000600050' and OrderNo like '300060%' and OrderNo not like '999999%']
com.ibm.db2.jcc.c.SqlException: DB2 SQL error: SQLCODE: -461, SQLSTATE: 42846, SQLERRMC: SYSIBM.DECIMAL;SYSIBM.VARCHAR
inner cast is ok
I can run it on my DB2 for i system (without the NVL(); not supported in my version).
Can you see if the outer cast will run when the inner one is cast to Decimal() instead of Numeric()?
ref: SQLState 42846 = "Cast from source type to target type is not supported."