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

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?

Related

Invalid collation "utf8_general_ci" in SQL Server SELECT statement

I am trying to running the below query:
SELECT TOP 1
*, (Title COLLATE utf8_general_ci)
FROM
BibTitles
WHERE
TitlesID > '.$last_iterate_book_id.'
ORDER BY
TitlesID ASC;
in PHP script. The above query is to get data from a SQL Server view.
When I am running the script, it is throwing the below error:
Invalid collation 'utf8_general_ci'
I am using the collation because, my database contains data in different language and showing me those data as question mark (???) instead of actual data. I can't alter the database as well as table due to permission issue.
Please help me with this.

Incorrect syntax near '<'

I have a task to get some code which is working correctly on SQL Server 2012 to work on SQL Server 2008 R2 as well. I got this error:
Additional information: Incorrect syntax near '<'
When I try to run my code I found out that something is wrong in this line of my SQL code
ALTER TABLE [dbo].[WorkTimeEntries]
ADD [TimeFinishedForRuntime] AS ISNULL([TimeFinished],
IIF ([TimeStarted] < SYSUTCDATETIME(), [dbo].[udf_GetCurrentDateTimeOffsetInTimeZone](DATENAME(TZOFFSET, [TimeStarted])), [TimeStarted]));
I have read that in this cases took place some kind of error when people try to get date, but I'm not sure what's wrong in my case.
There was no IIF in SQL Server 2008R2.
Replace it with CASE
ALTER TABLE [dbo].[WorkTimeEntries] ADD [TimeFinishedForRuntime] AS ISNULL(
[TimeFinished],
CASE WHEN [TimeStarted] < SYSUTCDATETIME() THEN [dbo].[udf_GetCurrentDateTimeOffsetInTimeZone](DATENAME(TZOFFSET, [TimeStarted])) ELSE [TimeStarted] END);
Basically, this error occurs if you any syntactical error in your SQL code. Please reverify all your statement sequence.

Server Explorer in Visual Studio(2010)

I'm working on Visual studio(2010) and using Server Explorer for database connectivity(SQL SERVER) . I am able to connect with it sql client easily, just the problem I'm facing is using the "WHERE" clause in sql query. When I write simple sql query like "Select * From Employee" then it is executing fine, but when I write "select * from Employee where EmpNo=1001 " Then it gives following error:
Error in WHERE clause near 'Employee'.
Unable to parse query text.
Then when I click continue the error that comes is:
SQL Execution Error:
Error source: .NetSqlClientDataProvider
Error Message: An expreesion of non-boolean type is provided where a condition is expected , near 'No'
Can someone please hepl with it..??
From your error message you must be putting a space between Emp and No.
If the EmpNo fieldname is two words in your table, surround it with [] eg; [Emp No]

PDO DBLib not working

I have a connection with the DBLIB PDO driver, and I am not getting any errors on connecting, but when I run a query an exception is thrown with the error below. I have played around with the syntax of the query as well. I am connecting to a MS SQL server:
SQLSTATE[HY000]: General error: 208 General SQL Server error: Check messages from the SQL Server [208] (severity 16) [SELECT PCO_INBOUNDLOG.PHONE FROM PCO_INBOUNDLOG]
The code:
$sql = "SELECT PCO_INBOUNDLOG.PHONE FROM PCO_INBOUNDLOG";
foreach($this->mssql->query($sql) as $row) {
print_r($row);
}
This is the first time I have ever done a query to a MS SQL server so my syntax may be wrong, any ideas?
First, find out what error 208 means:
select * from sys.messages where message_id = 208
Second, check the FROM syntax (including the examples!) and object identifier rules.
Third, write the query correctly:
SELECT PHONE FROM PCO_INBOUNDLOG
Or, probably better (because it's good practice to include the schema name):
SELECT PHONE FROM dbo.PCO_INBOUNDLOG
Or even:
SELECT p.PHONE FROM dbo.PCO_INBOUNDLOG p

Debugging "Incorrect Syntax" Exception

I have recently been working with a COM+ component that processes an input XML file, and makes a number of database updates based on the supplied data.
Running SQL Profiler with EventClass Exception, and User Error Message selected, I see:
Exception Error: 102, Severity: 15, State: 1
User Error Message Incorrect syntax near '3'.
However I really want to see the full SQL that is being supplied by the COM+ component.
Is there anyway for me using Profiler, or other, for me to intercept the SQL statement that was sent to the SQL Server?
This machine is using SQL Server 2005, and the COM+ object is written in Delphi.
In Profiler, try watching the events SP:StmtStarting and SQL:StmtStarting, and include column TextData in the output.
I think those events are only shown in the pick list if you select the "Show All Events" checkbox on the Events Selection tab.
The events needed are SQL:BatchStarting or SQL:BatchCompleted. The main confusing point is that the SQL is output AFTER the error messages - which does make sense when you think about it, but may not be intuitive.
As an example, with the query SELECT * FROM Table WHERE ID=3 8 (note the incorrect space between 3 and 8), I received the following output.
Exception Error: 102, Severity: 15, State: 1
User Error Message Incorrect syntax near '8'.
SQL:BatchStarting SELECT * FROM Table WHERE ID=3 8
SQL:BatchCompleted SELECT * FROM Table WHERE ID=3 8

Resources