0

I have a data transformation application where one of the first steps is for the user to paste in a valid SELECT statement. After it is verified, the app will execute it against their database and return the schema of the result set. They then tell us what they want us to do with the results.

My problem is that at one customer site, the user pastes in what appears to be a valid SQL statement but SqlCommand is throwing an error. They sent me a screenshot of the error message. The error is 'incorrect syntax near 'and'. However, the SQL statement looks fine to me and runs in SSMS.

The SQL statement is:

select
    id_acc,
    PaymentOption,
    ServiceCatDesc, 
    ServiceType, 
    format(c_custlocalstartdate,'MMMyyyy') as mmmyyyy,
    sum(c_debitamount) as debitamount, 
    sum(c_rateusage) as rateusage
from 
    netmeter..vw_usage_details 
where 
    id_usage_interval in (select id_interval 
                          from t_usage_interval 
                          where id_usage_cycle = 33 
                            and dt_start between dateadd(month, -12, getdate()) and getdate()) 
group by 
    id_acc, PaymentOption, ServiceCatDesc, ServiceType, 
    format(c_custlocalstartdate,'MMMyyyy')

This happens whether the customer is using a SQL Server driver or ODBC (either connecting to the same database).

Any ideas?

The SQL statement is valid and runs outside of the application. A lot of other queries run file in our application. Why is the database throwing a syntax error on this statement?

7
  • Is it treating the double )) for the closing of dateadd as an escape character since there are 2 of them? Commented Feb 16, 2023 at 13:15
  • 1
    The error indicates the actual query being executed is not the one in your question. Either the actual query text is different (e.g. non-displayable characters) or the code is changing the query before execution. Commented Feb 16, 2023 at 13:34
  • Is this a "parametrised" query? Commented Feb 16, 2023 at 13:39
  • Yeah the view definition could contain dynamic SQL? Commented Feb 16, 2023 at 13:59
  • Get the user to save the broken query to a file and send it to you as an email attachment. Stuff like this is almost always non-printable characters that get auto-magically "fixed' when copy-pasted into HTML mail messages and collaboration programs like Teams, etc.. Commented Feb 16, 2023 at 21:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.