I have the following the query running dynamically
SELECT * FROM Vehicles WHERE (DKID IN (69954)) ORDER BY case when ImageName1 = 'na' then 0 else 1 end, Make , Model, Year DESC
This is returning the following error:
Error Executing Database Query.
The query looks fine to me, does the query run in SqlServer Management Studio?
Do you have ImageName1 column in your table?
ImageName1 is in the table, and like the answers below state it does run fine outside of its dynamic form.
Try enclosing the
casestatement in parentheses.
works for me
here is repo script
are you running this query dynamically?, if so you might need to escape the quotes around ‘na’:
Your query works fine for me in SQL Mgmt Studio… Maybe try it this way instead to see if it gets you anywhere:
You’re using JDBC. Is there probably a transformation / interpretation from JDBC? Try making the ‘na’ a parameter. Check if there is a certain syntax in JDBC for string constants in queries. I don’t use JDBC, so I could be completely wrong.
As KMike said, it looks like you didn’t not escape properly.
Basically, when you ran your statement, it did not generate a syntactically correct SQL statement from the dynamic SQL.
Generally, when I am writing dynamic sql, I use a print statement to print the generated SQL. I can then review the generated sql visually for obvious mistakes, and then execute it it to make sure it works as expected.
If I made a mistake in the dynamic SQL, it will usually be revealed here.