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();
Related
I am using MSSQL server as DB and the method in the repository layer of my spring boot project:
List<Object[]> SummaryData(#Param("startDate") LocalDateTime startDate,
#Param("endDate") LocalDateTime endDate, List<Integer> cNoList);
here cNoList may contain only 0 or some values.
i want when cNoList contains values such as (45,30,20 etc.) then the below query will execute:
SELECT 'CUSTOMER' as title_key, COUNT(*) as total_count FROM LOGIN_TABLE lg left join USER_TABLE ut on lg.u_id = ut.tid_no where lg.user_type = 'CUSTOMER' and lg.is_verified = 1 and
lg.sys_time BETWEEN ?1 AND ?2 AND cId in (?3)
when cNoList contains only 0 then the last part of the above query --> "AND cId in (?3)" will not execute just like below:
SELECT 'CUSTOMER' as title_key, COUNT(*) as total_count FROM LOGIN_TABLE lg left join USER_TABLE ut on lg.u_id = ut.tid_no where lg.user_type = 'CUSTOMER' and lg.is_verified = 1 and lg.sys_time BETWEEN ?1 AND ?2
i have made an attempt like below:
"DECLARE #part VARCHAR(100) = ''; \n" +
"DECLARE #region_no INT = 420;\n" +
"DECLARE #start Datetime = '2022-07-01 00:00:00';\n" +
"DECLARE #end Datetime = '2023-01-17 23:59:59';\n" +
"DECLARE #tp VARCHAR(100) = 'CUSTOMER';\n" +
"SET #part = (CASE WHEN #region_no != 0 THEN 'and region_no = ' + CAST(#region_no AS VARCHAR(100)) ELSE '' END);\n" +
"EXEC ('SELECT ''' + #tp + ''' as title_key, COUNT(*) as total_count FROM LOGIN_TABLE lg left join USER_TABLE ut on lg.u_id = ut.tid_no where lg.user_type = ''' + #tp + ''' and lg.is_verified = 1 and \n" +
"lg.sys_time BETWEEN\n" +
"CONVERT(DATETIME, ''' + #start + ''', 120) \n" +
"AND\n" +
"CONVERT(DATETIME, ''' + #end + ''', 120) ' + #part)", nativeQuery = true)
List<Object[]> SummaryData(#Param("startDate") LocalDateTime startDate,
#Param("endDate") LocalDateTime endDate, List<Integer> cNoList);
this query executes well with the static value #region_no, #start, #end but these values will be assigned dynamically.
issues I am facing :
i tried putting a placeholder "?3" but not working. how can I put a placeholder like "?3" instead of 420 in this line "DECLARE #region_no INT = 420" ?
(the value of #region_no needs to be something of type array of int but i guess it is of type only int here)
how can i use the placeholder for "DECLARE #start Datetime = '2022-07-01 00:00:00';\n", i want to use something like
"DECLARE #start Datetime = ?1;"
You need to use :param instead of ?1.
For example:
"DECLARE #part VARCHAR(100) = ''; \n" +
"DECLARE #region_no INT = :cNoList;\n" +
"DECLARE #start Datetime = :startDate;\n" +
"DECLARE #end Datetime = :endDate;\n" +
"DECLARE #tp VARCHAR(100) = 'CUSTOMER';\n" +
"SET #part = (CASE WHEN #region_no != 0 THEN 'and region_no = ' + CAST(#region_no AS VARCHAR(100)) ELSE '' END);\n" +
"EXEC ('SELECT ''' + #tp + ''' as title_key, COUNT(*) as total_count FROM LOGIN_TABLE lg left join USER_TABLE ut " +
"on lg.u_id = ut.tid_no where lg.user_type = ''' + #tp + ''' and lg.is_verified = 1 and \n" +
"lg.sys_time BETWEEN\n" +
"CONVERT(DATETIME, ''' + #start + ''', 120) \n" +
"AND\n" +
"CONVERT(DATETIME, ''' + #end + ''', 120) ' + #part)", nativeQuery = true)
List<Object[]> SummaryData(#Param("startDate") LocalDateTime startDate,
#Param("endDate") LocalDateTime endDate,
#Param("cNoList") List<Integer> cNoList);
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();
I have a linq query
from c in db.Custommer
join m in db.Membership on c.ID equals m.CustomerID
select (c.LastName + ", " + c.FirstName + " " + c.MiddleName);
The MiddleName could be NULL, how do I replace that null with a space or ignore it?
If I leave it this way, the query does not return any records for customers who don't have middle names.
You can do as such:
from c in db.Custommer
join m in db.Membership on c.ID equals m.CustomerID
select (c.LastName + ", " + c.FirstName + " " + (c.MiddleName ?? "");
This should do the trick :)
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 ";
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.