Skattch
Skattch

Reputation: 9

SQL Server throwing error on valid SQL - incorrect syntax near 'and'

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?

Upvotes: 0

Views: 652

Answers (0)

Related Questions