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.
Related
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?
I am using the pypyodbc library to establish a connection to a SQL Server 2008 R2 database and every time I try to execute a .sql file I encounter the following error:
pypyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'Go'.")
This is the sql query I am trying to execute:
Use SL_Site1_App
Go
select emp_num,name, trans_num, job, trans_type
from Hours where trans_type like '1000%' order by trans_date desc
This is the python script that I am using:
import pypyodbc, ExcelFile
def main():
# read the SQL queries externally
queries = ['C:\\Temp\\Ready_to_use_queries\\Connection_sql_python.sql']
for index, query in enumerate(queries):
cursor = initiate_connection_db()
results = retrieve_results_query(cursor, query)
if index == 0:
ExcelFile.write_to_workbook(results)
print("The workbook has been created and data has been inserted.\n")
def initiate_connection_db():
connection_live_db = pypyodbc.connect(driver="{SQL Server}", server="xxx.xxx.xxx.xxx", uid="my-name",
pwd="try-and-guess", Trusted_Connection="No")
connection = connection_live_db.cursor()
return connection
The workaround for this problem is to delete the Use SL_Site1_App Go line but I want to know if this is a known problem related to the pypyodbc library to process these lines and if so, where should I look to notify the developers about this issue.
GO is a batch separator used by sqlcmd and SSMS. It's not a T-SQL operator.
Considering you're using an application to connect to SQL Server, declare your database in the connection string, by adding database="SL_Site1_App", and then remove the USE and GO statements in your SQL Statement.
Microsoft SQL Server 2008 R2
I am running a large SQL select query that may take hours to complete. So I try to break the query results into smaller sets.
e.g return results 1-10,000 first, then 10,001 - 20000, and so on
I used below code, but it gave me error
SELECT *
FROM PP_ConsolidatedSalesView
WHERE financial_period = '2018-11'
ORDER BY id
OFFSET 10000 ROWS
FETCH NEXT 10000 ROWS ONLY
I use a loop to dynamically change the offset and fetch next values.
The error message is:
Incorrect syntax near 'OFFSET'
Does anyone have an idea why? And is there an alternative solution?
Can you please confirm the database compatibility level. Offset is present in SQL Server 2012. If database is 2008 compatbility mode, then keyword isnt available.
You can check it like below:
USE AdventureWorks2012;
GO
SELECT compatibility_level
FROM sys.databases WHERE name = 'AdventureWorks2012';
GO
More info here: Incorrect syntax near OFFSET command
Through SAS/ACCESS, I can successfully run data steps querying external DBMS tables. E.g.,
Data OutTable;
Set ExternalDBMS.Table1;
Where Var1 ='abc';
Run;
However, when column name has space, it caused a problem even I used ''n.
One example as shown below:
Data OutTable;
Set ExternalDBMS.Table1;
Where 'Var 2'n ='abc';
Run;
ERROR: CLI open cursor error: [SAS][ODBC SQL Server Wire Protocol driver][Microsoft SQL Server]Incorrect syntax near the keyword 'Function'.
Further try with SAS Option validvarname=v7 to standardize the var names with spaces still caused same error.
After using SAS Option sastrace=',,,d' I found that SAS/ACCESS submitted statement to SQL server like this:
SELECT Var 1, .....
FROM schema1.Table1
WHERE (Var 1 ='abc' );
Apparently the code above would cause error in SQL server side because the Var 1 was neither quoted nor bracketed.
One way to fix it is using explicit pass-through query. I'm just wondering if there's any other ways to solve this problem too.
Thanks in advance!
when using an explicit pass-through query, put a set of square brackets around the variable name. This would be similar to how you'd write your code in SSMS.
SELECT [Var 1], ...
FROM schema1.Table1
WHERE ([Var 1] ='abc' );
I am curious to know what these two statements as displayed show a syntax error and cause a syntax error during runtime but run fine when run indepedently. Also adding or removing the semicolon displays a different syntax error in SSMS. Using SSMS 2014 with AventureWorks database. (Statement is for testing purposes. Don't pay attention to the values)
select * from Person.Address;
HumanResources.uspUpdateEmployeeHireInfo 2221,'d', '3/4/1992','3/4/1992', 3,3,1
It is specific to client (SSMS). You can execute stored proc without EXEC when the statement is single statement in the batch.
To avoid the error use:
select * from Person.Address;
EXEC HumanResources.uspUpdateEmployeeHireInfo 2221,'d', '3/4/1992','3/4/1992', 3,3,1