MY SQL SYNTAX ERROR HOW TO FIX IN SPRING BOOT - spring

Getting ERROR: java.sql.SQLSyntaxErrorException: 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 emp.employeeMangement.api.DTO.ResponseDTO(emp.empName,emp.gender,emp.email,emp at line 1
#Query(value = "select NEW com.emp.employeeMangement.api.DTO.ResponseDTO(" +
`"emp.empName," +
"emp.gender," +
"emp.email," +
"emp.empCode," +
"att.noOfAbsent," +
"att.noOfPresent," +
"att.month," +
"att.noOfDaysInMonth," +
"sal.salAmount," +
"round(#per_day\\:=sal.salAmount/att.noOfDaysInMonth) as salperday," +
"round(att.noOfPresent*#per_day) as Salarycurrentmonth " +
") " +
"from Employee as emp " +
"join Attendence as att " +
"on emp.emp_id=att.cp_fk" +
" left join Salary as sal " +
"on sal.emp_id_fk=emp.emp_id",nativeQuery = true)
List<ResponseDTO> getInfoInExcel();

Related

Equivalent of Select #variable in Spring Data Jpa

I need to run a long query on a spring data jpa method call.
What is the equivalent of Select #sql in spring jpa.
I am getting exception when i try to execute it as a native query.
Below is the query that is working fine in SQL server
Declare #sql varchar(max) = 'select '
select #sql = #sql + 'sum(' + c.name + ')' + ' AS ' + c.name + ','
from sys.columns c
inner join sys.tables t on c.object_id = t.object_id
inner join sys.types ty on
c.user_type_id = ty.user_type_id
where
ty.name in ('bigint') and
t.name = 'my_used_features'
select #sql = LEFT(#sql,LEN(#sql)-1) + ' from my_used_features '
EXEC (#sql)
I want to write the same query as native query in my repository class.But I am getting exception like ....... EXEC (#sql)> starts a quoted range at 342, but never ends it.I am not able to figure out the issue.Can anyone please help me on what am I doing wrong.
#Query
(value="Declare #sql varchar(max) = 'select '\r\n"
+ " \r\n"
+ " select #sql = #sql + 'sum(' + c.name + ')' + ' AS ' + c.name + ',' \r\n"
+ " from sys.columns c\r\n"
+ " inner join sys.tables t on c.object_id = t.object_id\r\n"
+ " inner join sys.types ty on\r\n"
+ " c.user_type_id = ty.user_type_id\r\n"
+ " where\r\n"
+ " ty.name in ('bigint') and\r\n"
+ " t.name = 'my_used_features'\r\n"
+ " \r\n"
+ " select #sql = LEFT(#sql,LEN(#sql)-1) + ' from my_used_features ' \r\n"
+ " \r\n"
+ " EXEC (#sql)",nativeQuery=true)
Map<String,Integer> findSum();

JPA #Query SpEL throwing EL1007E despite null checks

I have this query:
#Query(value =
"select distinct t from TimesheetReport t "
+ "where t.month = :month and t.year = :year and t.approvedByMaster = false "
+ "and (:#{#rule.employee} is null or :#{#rule.employee.id} is null "
+ "or t.account.id = :#{#rule.employee.id}) "
+ "and t.id in "
+ "(select distinct dt.timesheetReport.id from DailyTime dt "
+ "where (:#{#rule.project} is null or :#{#rule.project.id} is null "
+ "or dt.project.id = :#{#rule.project.id}) "
+ "and (:#{#rule.client} is null or :#{#rule.client.id} is null "
+ "or dt.client.id = :#{#rule.client.id})) "
+ "or t.id in "
+ "(select distinct cdt.timesheetReport.id from CustomDailyTime cdt "
+ "where (:#{#rule.project} is null or :#{#rule.project.id} is null "
+ "or cdt.project.id = :#{#rule.project.id}) "
+ "and (:#{#rule.client} is null or :#{#rule.client.id} is null "
+ "or cdt.client.id = :#{#rule.client.id}) "
+ ")"
)
Set<TimesheetReport> findTimesheetsMatchingManualApprovalRule(LeaderManualApprovalRulesDTO rule, int month, int year);
I know this is caused by employee object being null.
This happens despite first checking if employee in rule is null and then checking if employee object id field is null. I still get "Property or field 'id' cannot be found on null".
Is it because SpEL needs to be able to evalute every parameter before firing a query?

Error missing ')' at Oracle NoSQL table creating table

I have the following error when trying to create this table
java.lang.IllegalArgumentException: Error: at (1, 1140) missing ')' at
',', at line 1:1140
"create table if not exists " + TABLE_NAME +
"(id string, " +
"subId string, " +
"startTime timestamp(4), " +
"timeId string, " +
"primary key(id, shard(subId, startTime)))";
Any ideas in how to solve this
The error message is not clear but your issue is because the shard(...) should be the first thing after primary key (, and , id should come after the shard key.
"create table if not exists " + TABLE_NAME +
"(id string, " +
"subId string, " +
"startTime timestamp(4), " +
"timeId string, " +
"primary key(shard(subId, startTime), id))";

SpringJDBC Gives ORA-00933: SQL command not properly ended But Query Runs OK in DB Client

The following Oracle query runs OK in my DB Client, PL/SQL Developer, and returns 1 result.
On running it via NamedParameterJdbcTemplate (SpringJDBC) in my Java app, I get
java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended
There can't be any space issues or anything obvious, because this exact query completes in PL/SQL.
private static final String SELECT1 =
" SELECT COUNT(*) "
" FROM table1 t1, table2 t2 " +
" WHERE t1.received_date > TRUNC(sysdate - 1) " +
" AND t1.received_date < TRUNC(sysdate) " +
" AND t1.type IN ('TYPE1', 'TYPE2') " +
" AND t2.received_num = t1.received_num; ";
public int getSelect1() {
HashMap<String,Object> paramMap = new HashMap<String,Object>();
return jdbcTemplate.queryForObject(SELECT1, paramMap, Integer.class);
}
I think you don't require the semi colon with the sql string.
private static final String SELECT1 =
" SELECT COUNT(*) " +
" FROM table1 t1, table2 t2 " +
" WHERE t1.received_date > TRUNC(sysdate - 1) " +
" AND t1.received_date < TRUNC(sysdate) " +
" AND t1.type IN ('TYPE1', 'TYPE2') " +
" AND t2.received_num = t1.received_num ";

how to use order by in sql select query to sort complaint no in ascending order

qry1 = "Select * from ser_complaint_master a,ser_complaint_status b,company_master c
where a.complaint_no=b.complaint_no
and a.allocation_code=c.co_code
and c.co_br_code='" + Session["BRCODE"] + "'
and a.Complaint_Date>='" + Frdat + "' and a.Complaint_Date<='" + Todat + "'
and a.status in ('Completed')
and a.complaint_type in('" + cmptype + "')";
How to use ORDER BY in select query if more than one tables are involved.
Add order by a.complaint_no to the end of the query.

Resources