syntax error at or near "ORDER" in hibernate - sql

I have an query that I want to execute via persistence.query in hibernate.
final String StatusQuery = convertStringFiltersToQuery(StatusFilters);
sql = String.format("Select * FROM Program p LEFT JOIN p.user vt LEFT JOIN vt.Vendor LEFT JOIN p.Program WHERE vt.Status IN (%s) ORDER BY ID DESC",
StatusQuery);
entityManager.createNativeQuery(sql).getResultList();
But When I try to execute it, I got error like
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at
or near "ORDER"
Anything I am missing here ?
Thanks in Advance !!
Updated the question after got the suggestion from below answer:
Now have changed the query but seems the same issue happened:
sql = String.format("Select * FROM Program p LEFT JOIN p.user vt LEFT JOIN vt.Vendor LEFT JOIN p.Program on (p.program_id = p.UserProgram.program_id) WHERE vt.Status IN (%s) ORDER BY ID DESC",
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "ORDER"

Looks like you'll need a condition to join . These syntax errors at or near 'x' is usually pointing to an error just before x, in this case, the LEFT JOIN has no ON clause.
Edit: Response to question update, you'll need conditions for each LEFT JOIN clause, here's an example, and here.

Related

Syntax error on join operation access SQL

I'm trying to do some join operations in sql access but I keep on getting the join operation error. At first it was just the JOIN alone, but then I realized I had to add the INNER which I did but it didn't resolve the error.
Code below:
SELECT Formula.*, Ingred.[Europe Ban]
FROM [Ingred]
INNER JOIN Ingred ON Formula.[Ingredient] = Ingred.Ingredients;
Presumably, you want Formula in the FROM clause, not Ingred twice:
SELECT Formula.*, Ingred.[Europe Ban]
FROM Formula INNER JOIN
Ingred
ON Formula.[Ingredient] = Ingred.Ingredients;

ASP NET SqlException: Incorrect Syntax

I have a method in my ASP NET webpage to execute a SQL command. When I execute the following command I am getting
SqlException was unhandled by user code
The SQL error is
Incorrect syntax near Chemicals'
Here is the SQL command string:
SELECT MainHeading
FROM Areas
INNERJOIN Chemicals ON Areas.AreaNo = Chemicals.AreaNo
WHERE (Chemicals.IDMark = #item)
Where #item is a session variable.
The simplified tables are as follows
You missed a space between innerjoin
SELECT MainHeading
FROM Areas
INNER JOIN Chemicals ON Areas.AreaNo = Chemicals.AreaNo
WHERE (Chemicals.IDMark = #item)
It must be:
SELECT MainHeading FROM Areas INNER JOIN Chemicals ON Areas.AreaNo = Chemicals.AreaNo WHERE (Chemicals.IDMark = #item)
INNER JOIN were two words :)
As an information for you, INNER JOIN is the same as JOIN.

Oracle Invalid Identifier ORA-00904

I keep getting this error trying to run this simple Join.....
SELECT docregitem.reviewdate, docregitem.nclient, client.name
FROM docregitem, client
INNER JOIN client
ON docregitem.nclient = client.nclient
ORA-00904: "DOCREGITEM"."NCLIENT": invalid identifier
I can do a select and all the columns are present and correct...
I think the query you want is:
SELECT dr.reviewdate, dr.nclient, c.name
FROM docregitem dr INNER JOIN
client c
ON dr.nclient = c.nclient;
Your from clause has a comma in it. This is a lot like a cross join, but it affects the columns. These are not known in the on clause, which is what is causing the problem.
SELECT docregitem.reviewdate, docregitem.nclient, client.name
FROM docregitem
INNER JOIN client
ON docregitem.nclient = client.nclient
you try this one as you used client table twice one with simple join and other with inner join without giving the alias name to the table so the compiler is confused in selecting and comparing column from client table.

Two INNER JOIN ODBC MSAccess exception with query

I have a problem.. I want to connect to my database with odbc to msAccess from c# app. I have got exception error. Please my query... It should work but apparently I am doing something in wrong way here..
ERROR [42000] [Microsoft][ODBC Microsoft Access Driver] Syntax error
(missing operator) in query expression
'[autoResults].[autoStats]=[autoStats].ID INNER JOIN [users] ON
[users].ID=[autoStats].userID
string queryString =#"
SELECT [erNumber].[autoResults],
[passedTesting].[autoResults],
[releaseVersion].[autoStats],
[lastFunction].[autoResults],
[startTime].[autoStats],
[Name].[users]
FROM [autoStats]
INNER JOIN [autoResults]
ON [autoResults].[autoStats]=[autoStats].ID
INNER JOIN [users]
ON [users].ID=[autoStats].userID
WHERE [erNumber].[autoResults] LIKE '" + TextBox1.Text + "'";
Access can be fussy about multiple JOINs and often requires that they be enclosed in parentheses. So instead of
FROM [autoStats]
INNER JOIN [autoResults]
ON [autoResults].[autoStats]=[autoStats].ID
INNER JOIN [users]
ON [users].ID=[autoStats].userID
try
FROM
(
[autoStats]
INNER JOIN
[autoResults]
ON [autoResults].[autoStats]=[autoStats].ID
)
INNER JOIN
[users]
ON [users].ID=[autoStats].userID

Nested query issue, Invalid Identifier error

I am running following query in Oracle 10 XE and getting [Err] ORA-00904: "Q": invalid identifier
SELECT questions.sb_title Q,questions.sb_baab_id,questions.sb_fasal_id,QUESTIONS.SB_CONTENT answer,IST.SB_PREFERENCE PREF
FROM USER_QUESTIONS questions
INNER JOIN USER
ON QUESTIONS.SB_USER_ID = USER.SB_ID
INNER JOIN IST
ON IST.SB_ID = USER.SB_IST_ID
AND(Q LIKE '%where is%')
AND USER.SB_IST_ID =
(
Select issued.SB_IST_ID
FROM USER_REGISTER register_number
INNER JOIN USER_ISSUED issued
ON register_number.SB_REGISTER_NUMBER = ISSUED.SB_REGISTER_NUMBER
AND REGISTER_NUMBER.SB_REGISTER_NUMBER IN(1240)
)
You cannot reference an alias in the JOIN condition - write the actual column name: sb_title.
I don't think you can use the alias "Q" like that. You're aliasing it in your select clause, don't think you can use that alias in your ON clause. Try using the full column name (sb_title).