Syntax error on multiple join statement - sql

Hello, I'm getting a syntax error on this sql statement, can anyone advise thanks
String sql = "Select tblStudent.*,tblSchool.*,tblAgents.* " +
"FROM tblStudent LEFT JOIN tblSchool " +
"ON (tblStudent.schoolID = tblSchool.schoolID) " +
"LEFT JOIN tblAgents " +
"ON (tblStudent.agentID = tblAgents.agentID) " +
"WHERE tblStudent.StudentID='" + studentID + "'";
I was hoping that I could do multiple joins
But I am getting a syntax error.

For access, parenthesis with multiple joins means the following. If you have three joins, there are two left parenthesis after the from. The last join does not have a right parenthesis.
String sql = "Select tblStudent.*,tblSchool.*,tblAgents.* " +
"FROM (tblStudent LEFT JOIN tblSchool " +
"ON (tblStudent.schoolID = tblSchool.schoolID)) " +
"LEFT JOIN tblAgents " +
"ON (tblStudent.agentID = tblAgents.agentID) " +
"WHERE tblStudent.StudentID='" + studentID + "'";
Access SQL injection has been covered in other threads .

String sql = "Select
tblStudent.StudentID,tblStudent.studentFirstName,"+
tblStudent.studentLastName,tblSchool.schoolName," +
tblAgents.agentFirstName,tblAgents.agentLastName " +
"FROM (tblStudent LEFT JOIN tblSchool " +
"ON (tblStudent.schoolID = tblSchool.schoolID)) " +
"LEFT JOIN tblAgents " +
"ON (tblStudent.agentID = tblAgents.agentID) " +
"WHERE tblStudent.StudentID=#studentID";

I believe your final SQL should look like this:
SELECT tblStudent.*
,tblSchool.*
,tblAgents.*
FROM tblSchool
RIGHT JOIN (
tblAgents RIGHT JOIN tblStudent ON tblAgents.agentID = tblStudent.agentID
) ON tblSchool.schoolID = tblStudent.schoolID
WHERE tblStudent.StudentID=111;
So, the VBA code for creating this SQL should be
Dim sql As String
sql = "SELECT tblStudent.* ,tblSchool.* ,tblAgents.* " & _
"FROM tblSchool RIGHT JOIN (" & _
"tblAgents RIGHT JOIN tblStudent ON tblAgents.agentID = tblStudent.agentID" & _
") ON tblSchool.schoolID = tblStudent.schoolID " & _
"WHERE tblStudent.StudentID=" & studentID
Here I assume that studentID is numeric field. Also I would recommend to do not use * for selecting the data from more than one table, otherwise column names may be unpredictable and as mentioned in comments, it will require additional resources, which won't be used.

Related

"ORA-00920: invalid relational operator" Error

select
ic.item_name,
lh.locn_brcd from_locn,
lh2.locn_brcd to_locn,
wl.from_container,
wl.to_container,
wl.units,
wl.prev_from_container_status prev_from_lpn_status,
wl.curr_from_container_status curr_from_lpn_status,
wl.prev_to_container_status prev_to_lpn_status,
wl.curr_to_container_status curr_to_lpn_status,
wl.work_batch_number,
wl.transaction_name,
wl.action,
wl.work_id,
wl.date_updated,
wl.source_updated,
wl.tote_number,
wl.chute
from m_work_log wl
LEFT join item_cbo ic on wl.item_id=ic.item_id
left join locn_hdr lh on wl.from_location_id = lh.locn_id
left join locn_hdr lh2 on wl.to_location_id = lh2.locn_id
where wl.action in (:action)
and trunc(wl.date_updated) between :start_date and :end_date
and (ic.item_name in (:list) OR
wl.source_updated = :username OR
wl.to_container in (:LPNList) OR
(:list is null and :username is null and :LPNList is null)
)
order by date_updated desc
Hi everyone,
when I run this code through Oracle SQL Developer and I add two items to the :list parameter and two items to the :action parameter it works fine. But when I run this through SSRS (report builder ) it fails to run and I get an "ORA-00920: invalid relational operator". I'm new to SQL and i'm not sure what I am doing incorrectly here. Any help is greatly appreciated. Thanks!
There are 2 ways to do this:
Multiple Value Parameter:
First of all, you must use Oracle provider, multiple value paramaters does not work with neither ODBC nor OLEDB connections (reference).
Here is an external link explaining in detail here.
Using an expression as the query by putting the whole thing like this ="query_here"
="select "
+ " ic.item_name,"
+ " lh.locn_brcd from_locn,"
+ " lh2.locn_brcd to_locn,"
+ " wl.from_container,"
+ " wl.to_container,"
+ " wl.units,"
+ " wl.prev_from_container_status prev_from_lpn_status,"
+ " wl.curr_from_container_status curr_from_lpn_status,"
+ " wl.prev_to_container_status prev_to_lpn_status,"
+ " wl.curr_to_container_status curr_to_lpn_status,"
+ " wl.work_batch_number,"
+ " wl.transaction_name,"
+ " wl.action,"
+ " wl.work_id,"
+ " wl.date_updated,"
+ " wl.source_updated,"
+ " wl.tote_number,"
+ " wl.chute"
+ "from m_work_log wl"
+ " LEFT join item_cbo ic on wl.item_id=ic.item_id"
+ " left join locn_hdr lh on wl.from_location_id = lh.locn_id"
+ " left join locn_hdr lh2 on wl.to_location_id = lh2.locn_id"
+ "where wl.action in (:action)"
+ " and trunc(wl.date_updated) between :start_date and :end_date"
+ " and (ic.item_name in ('" + Join(Parameters!list.Value , "', '")" + ') OR"
+ " wl.source_updated = :username OR"
+ " wl.to_container in ('" + Join(Parameters!LPNList.Value , "', '")" + ') OR"
+ " (:list = '_N/A_' and :username is null and :LPNList = '_N/A_')"
+ " )"
+ " order by date_updated desc"
In this case you will need to provide default empty values to your lists. I used 'N/A' in my example.

#query with named #param returning empty result set when using sql IN

Btw this is already working with a jdbc template based solution that I wrote. It's not me wanting to do the rewrite, it is the tec lead's idea. It is giving me a headache.
Lets not get into the discussion on why this is or isn't a good idea, but rather how to make it work.
in my repo
#Repository
interface Foo...extends JpaRepository
this works:
#Query(value = "SELECT t.td, em.ct, j.dsc AS bs, ct.dsc AS ct_name, "
+ "t.tn, et.dsc, f.f_id, f.f_life, f.fl_name, em.f_note "
+ "FROM ext_master em join tax t on em.td = t.td "
+ "join bs j on j.bs = t.bs "
+ "join ex_type et on em.ex_type = et.ex_type "
+ "join ct ct on ct.ct = em.ct "
+ "left join ex_f ef on em.td = ef.td and em.ct = ef.ct "
+ "left join f f on ef.f_id = f.f_id "
+ "WHERE em.ct in ( 'CC' ) "
+ "AND t.state IN ('FF','AK','AL','GA')"
+ "ORDER BY bs, t.tn", nativeQuery = true)
List<Object> getExtensionsByCustTypeAndState(#Param("custTypes") String custTypes, #Param("states") String states);
this doesn't returns empty result set
#Query(value = "SELECT t.td, em.ct, j.dsc AS bs, ct.dsc AS ct_name, "
+ "t.tn, et.dsc, f.f_id, f.f_life, f.fl_name, em.f_note "
+ "FROM extension_master em join tax t on em.td = t.td "
+ "join bs j on j.bs = t.bs "
+ "join exemption_type et on em.ex_type = et.ex_type "
+ "join ct ct on ct.ct = em.ct "
+ "left join ex_f ef on em.td = ef.td and em.ct = ef.ct "
+ "left join f f on ef.f_id = f.f_id "
+ "WHERE em.ct in ( :custTypes ) "
+ "AND t.state IN ('FF', :states)"
+ "ORDER BY bs, t.tn", nativeQuery = true)
List<Object> getExByCustTypeAndState(#Param("custTypes") String custTypes, #Param("states") String states);
both can have one or multiple values, I tried:
with both params like {:states} :#{#states}
with the value inside the param
surrounded by " or by '
passing the whole line in the parameter like "AND t.state IN ('FF','AK','AL','GA')"
passing the 'FF','AK','AL','GA' in the states param and subtituting the in the query with
+ "AND t.state IN ( :states)"
I did write a simple query to make sure I am getting the parameters passed in right. so that's working.
Can anyone write this right, so that the in statements work, or tell me why it is not possible.
Thank you.
You pass all the possible values in one parameter that's why you don't get the results you are expecting.
You have to change the where clause like this:
"WHERE em.ct in ( :custTypes1, :custTypes2, :custTypes3 .... :custTypesN) "
this ended up being the simplest solution
instead of :
"WHERE em.ct in ( :custTypes ) "
+ "AND t.state IN ('FF', :states)"
do:
"WHERE em.ct in :custTypes "
+ "AND t.state IN :states "
and add the FF to the states string on before I call the function.

Springboot Hibernate Query: filter by childrens attributes

Hi I have a problem generating a #query in my SpringBoot repo. Using hibernate.
My POJOs:
class Offer
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
#ManyToMany
private List<Attribute> attributes;
#ManyToMany
private List<Offer> offers;
and
class Attribute
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
It works this way to get the "offer.offers by attribute". But now I want to enhance my query to get "offer offer.offers.attribute" an I strugle with it.
What I want is to find all Offers which have "Offer.attribute == 1" AND "all Offer.offers.attribute == 2"
Ending with this query:
#query
"SELECT o FROM Offer o "
+ "JOIN o.attributes a "
+ "LEFT OUTER JOIN o.offers oo "
+ "LEFT OUTER JOIN o.offers.attributes ooa " <-- Problem issue
+ "WHERE o.status = 1 "
+ "AND oo.status = 1 "
+ "AND a.id = 100 "
+ "AND ooa.id = 101 "
+ "GROUP BY o.id "
But upon compilation I get this exception:
org.hibernate.QueryException: illegal attempt to dereference collection
[offer0_.id.offers] with element property reference [attributes]
Now I wonder how to do it correctly?
Hope some SQL/Hibernate hero can help me.
Thanks in advance!
Gregor
Well... Iam one step further:
this query works:
#query
"SELECT o FROM Offer o "
+ "JOIN o.attributes a "
+ "LEFT OUTER JOIN o.offers oo "
+ "LEFT OUTER JOIN oo.attributes ooa " <-- referencin JOIN
+ "WHERE o.status = 1 "
+ "AND oo.status = 1 "
+ "AND a.id = 100 "
+ "AND ooa.id = 101 "
+ "GROUP BY o.id "
But now I get all results with at least on "Offers.offer.attributes == 101"
But I need all offers.offer having that attribute.
Any hints how to continue?

"where" restrictions not working on hql query with join clause

First of all clarify that I am quite bad with databases, so please do not be to mean with my code :P
I have a problem with a query on hibernate using join and restrictions. I have a huge list of Assignments and some of them have an Asr object.
queryString.append("select new commons.bo.assignment.AssignmentAssociateExtract("
+ " assignment.id, assignment.contract.assignmentStatus,"
+ " assignment.contract.beginDate, assignment.contract.endDate,"
+ " assignment.contract.contractType, assignment.organizationalData.homeCountryKey,"
+ " assignment.organizationalData.hostCountryKey,"
+ " assignment.organizationalData.homeOrgUnitKey,"
+ " assignment.associate.globalIdAssociate,"
+ " assignment.associate.localIdHome,"
+ " assignment.associate.firstName,"
+ " assignment.associate.lastName)"
+ " from Assignment assignment left join assignment.asr asr"
+ " where assignment.contract.assignmentStatus.code = 5"
+ " and asr.homeAsrElegibility is not 'X'"
+ " or asr.homeAsrElegibility is null"
);
I was creating this query step by step. Before I created it without the join clause left join assignment.asr asr and it was working well but of course it was not showing the Assignments that did not have a Asr object. After I added the join clause, now it shows every single Assignment (10.000 records when those who have an assignmentStatus = 5 are just 4.000) and the restrictions like
where assignment.contract.assignmentStatus.code = 5
are not reflected in the result anymore.
So to sum up: I need a list with all assignments with assignmentStatus = 5 and asr.homeAsrElegibility != 'X'. But it needs to include also all assignments with assignmentStatus = 5 even if they do not have an Asr object.
Any ideas?? Thanks!
Parenthesis helps to clarify the situation.
queryString.append("select new commons.bo.assignment.AssignmentAssociateExtract("
+ " assignment.id, assignment.contract.assignmentStatus,"
+ " assignment.contract.beginDate, assignment.contract.endDate,"
+ " assignment.contract.contractType, assignment.organizationalData.homeCountryKey,"
+ " assignment.organizationalData.hostCountryKey,"
+ " assignment.organizationalData.homeOrgUnitKey,"
+ " assignment.associate.globalIdAssociate,"
+ " assignment.associate.localIdHome,"
+ " assignment.associate.firstName,"
+ " assignment.associate.lastName)"
+ " from Assignment assignment left join assignment.asr asr"
+ " where assignment.contract.assignmentStatus.code = 5"
+ " and ((asr.homeAsrElegibility is not null and asr.homeAsrElegibility is not 'X')"
+ " or (asr.homeAsrElegibility is null))"
);

SQL incorrect syntax near a table name after FROM and before INNER JOIN.

Hi I have this SELECT query. I've tried excuting the query on the SQL Pane on Visual Studio 2008 and it works. However when I run the page (this is an asp.net page), it throws an SQL Exception saying I have an incorrect syntax near Schedules.
string selectSchedString = "SELECT Subjects.subject_title, Schedules.class_day, CAST(MIN(Schedules.time_in) AS varchar(10)) + ' - ' + CAST(MAX(Schedules.time_out) AS varchar(10)) AS Expr1" +
"FROM Schedules "+ //The exception points here
"INNER JOIN Subjects ON Schedules.subject_id = Subjects.subject_id " +
"INNER JOIN Student ON Student.section_id = " + currentSection + " " +
"GROUP BY Subjects.subject_title, Schedules.class_day";
Any ideas? As I've said, I tried excuting this on the SQL pane and it worked. Is there any special condition in asp.net that I've missed or something?
You are missing any white space between the end of the first line and FROM
Change AS Expr1" to AS Expr1 "
This is because the concatinated string is not correct try:
string selectSchedString = "SELECT Subjects.subject_title, Schedules.class_day, CAST(MIN(Schedules.time_in) AS varchar(10)) + ' - ' + CAST(MAX(Schedules.time_out) AS varchar(10)) AS Expr1" +
" FROM Schedules "+ //The exception points here
" INNER JOIN Subjects ON Schedules.subject_id = Subjects.subject_id " +
" INNER JOIN Student ON Student.section_id = " + currentSection + " " +
" GROUP BY Subjects.subject_title, Schedules.class_day";
Notice the empty spaces.