FreeTDS ODBC error "The identifier that starts with ... is too long. Maximum length is 128." - sql

I've been using pyodbc and free TDS to a submit queries to a microsoft SQL-2012 without issue for a while now but have recently stumbled on an issue.
When I submit a longer query I get an error message saying that one of the identifiers is too long and the script crashes.
The EXACT same query works in management studio and doesn't even return a warning.
I've added the following to my code with no avail.
SET QUOTED_IDENTIFIER OFF
SET ANSI_NULLS ON
The piece of code that the error is referring to is here:
(ZR_KOPF.ZRK_DDP_B IN ('190/TEMP.AA1H', '190/RAIN.AA1H', '190/WDR.AA1H', '190/WSP.AA1H', '190/SD1.AA1H', '190/HUMID.AA1H', '190/NO2.AA1H', '190/PM10.AA1H', '190/PM2.5.AA1H', '190/SO2.AA1H', '190/OZONE.AA1H', '190/CO.AA1H', '206/TEMP.AA1H', '206/RAIN.AA1H', '206/WDR.AA1H', '206/WSP.AA1H', '206/SD1.AA1H', '206/HUMID.AA1H', '206/NO2.AA1H', '206/PM10.AA1H', '206/PM2.5.AA1H', '206/SO2.AA1H', '206/OZONE.AA1H', '206/CO.AA1H')) AND ZR_KOPF.ZRK_BASEPRODUCTION = 1)
The error I get is:
pyodbc.ProgrammingError: ('42000', "[42000] [FreeTDS][SQL Server]The identifier that starts with ''190/TEMP.AA1H', '190/RAIN.AA1H', '190/WDR.AA1H', '190/WSP.AA1H', '190/SD1.AA1H', '190/HUMID.AA1H', '190/NO2.AA1H', '190/PM10.AA' is too long. Maximum length is 128. (103) (SQLExecDirectW)")
Pyodbc version: 4.0.18b1
freetds version: 1.00.48
Any help would be much appreciated
Cheers!

So turns out the spaces between each of the items in the list made all the difference...
I removed them and the query worked without a hitch.
Thanks for all the help everyone!

Related

Runtime error : DBSQL_SQL_ERROR exception : CX_SY_OPEN_SQL_DB, where to focus?

More info from stms :
Short Text
SQL error "SQL code: -10108" occurred while accessing table "CDHDR".
What happened?
Database error text: "SQL message: Session has been reconnected."
Return value of the database layer: "SQL dbsl rc: 99"
The problem is that client is running this code through the job in which I am not available to debug it. All I know is that it is working for other countries selected but for some specific it is not so the question is :
Was it a temporary connection error between two servers or is it data overload issue due to SELECT statement?
At the moment I am not really sure which thing I have to look on the system, the same program once produced the error for exceeding the limit in the db query ( > 10 minutes) so this might be related to system configuration or what?
Thanks in advance

[Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near '='

I have started getting an error on a page and am really struggling to work out the cause of this. I have searched on Stack Overflow for this same error message and found some people that got it also, but there solutions were very different to mine. This is the error message I'm getting:
Microsoft OLE DB Provider for ODBC Drivers error '80040e14'
[Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near '='.
/test-page.asp, line 97
This related to this line on that .asp page:
set rsMainContact=oConn.execute
("SELECT * FROM tbl_individual WHERE individual_id="&iMainContact)
Now the strange thing about this is that the page is actually bringing up the correct data, it's getting all the data it needs from that table. Which is why I don't understand why there's a problem.
Also, when I run the query using the "SELECT * FROM tbl_individual WHERE individual_id="&iMainContact" statement in MSSQL, replacing &iMainContact for an actual variable, it works absolutely fine.
Does anyone have any idea why this error might be occurring?
In case you want to know where the various variables come from then this is also some relevant code from the page:
SET rsOrganisationPendingOrganic=Server.CreateObject("ADODB.RecordSet")
rsOrganisationPendingOrganic.CursorType=3
rsOrganisationPendingOrganic.Open sSQL, oConn
iOrganisationPendingOrganicCount=rsOrganisationPendingOrganic.RecordCount
iMainContact=rsOrganisationPendingOrganic("organisation_maincontact")
if the following code is run BEFORE the error occurs, you most likely have no value for iMainContact:
SET rsOrganisationPendingOrganic=Server.CreateObject("ADODB.RecordSet")
rsOrganisationPendingOrganic.CursorType=3
rsOrganisationPendingOrganic.Open sSQL, oConn
iOrganisationPendingOrganicCount=rsOrganisationPendingOrganic.RecordCount
iMainContact=rsOrganisationPendingOrganic("organisation_maincontact")
you can prove this by writing out the sql to the screen before executing the failing sql:
Response.Write "SELECT * FROM tbl_individual WHERE individual_id=" & iMainContact
Response.End
I also agree 1000% with lad2025, use parameterized queries to guard against sql injection

updateblob fails in Powerbuilder

In Powerbuilder I am trying to update a table (Oracle) with blob but get sqlerror, "Database statement must refer to blob variable". My declaration and updateblob statements are as follows:
blob lblob_newxml
long llong_subid
UPDATEBLOB RP_XML_FORMS SET XML_DOC = :lblob_newxml
WHERE SUBMISSION_ID = :llong_subid
USING SQLCA;
Does anybody know why it is happening and or how to solve this problem? Thanks.
To get more information on this problem and the possible causes, I'd run with one of the database traces turned on. (You can check out database trace options in the Connecting to Your Database manual; link may not be appropriate for your PB version, which you haven't mentioned yet.) This may or may not tell you more, but it tracks everything between the app and when the PB drivers pass the commands "over the wall" to the database's driver.
Good luck,
Terry.
"The PowerBuilder VM can get the SQL syntax for the following types of errors, and passes it to the Transaction object’s DBError event for the following types of errors: ..." (see this page).
If your lblob_newxml is null then use this update statement instead:
UPDATE RP_XML_FORMS SET XML_DOC = NULL
WHERE SUBMISSION_ID = :llong_subid
USING SQLCA;

SQL STATE 37000 [Microsoft][ODBC Microsoft Access Driver] Syntax Error or Access Violation

Good day!
I get this error:
SQL STATE 37000 [Microsoft][ODBC Microsoft Access Driver] Syntax Error
or Access Violation, when trying to run an embedded SQL statement on
Powerscript.
I am using MsSQL Server 2008 and PowerBuilder 10.5, the OS is Windows 7. I was able to determine one of the queries that is causing the problem:
SELECT top 1 CONVERT(DATETIME,:ls_datetime)
into :ldtme_datetime
from employee_information
USING SQLCA;
if SQLCA.SQLCODE = -1 then
Messagebox('SQL ERROR',SQLCA.SQLERRTEXT)
return -1
end if
I was able to come up with a solution to this by just using the datetime() function of PowerBuilder. But there are other parts of the program that is causing this and I am having a hard time in identifying which part of the program causes this. I find this very weird because I am running the same scripts here in my dev-pc with no problems at all, but when trying to run the program on my client's workstation I am getting this error. I haven't found any differences in the workstation and my dev-pc. I also tried following the instructions here, but the problem still occurs.
UPDATE: I was able to identify the other script that is causing the problem:
/////////////////////////////////////////////////////////////////////////////
// f_datediff
// Computes the time difference (in number of minutes) between adtme_datefrom and adtme_dateto
////////////////////////////
decimal ld_time_diff
SELECT top 1 DATEDIFF(MINUTE,:adtme_datefrom,:adtme_dateto)
into :ld_time_diff
FROM EMPLOYEE_INFORMATION
USING SQLCA;
if SQLCA.SQLCODE = -1 then
Messagebox('SQL ERROR',SQLCA.SQLERRTEXT)
return -1
end if
return ld_time_diff
Seems like passing datetime variables causes the error above. Other scripts are working fine.
Create a transaction user object inherited trom transaction.
Put logic in the sqlpreview of your object to capture and log the sql statement being sent to the db.
Instantiate it, connect to the db, and use it in your embedded sql.
Assuming the user gets the error you can then check what was being sent to the db and go from there.
The error in your first statement should be the second parameter to CONVERT function.
It's type is not a string, it's type is an valid expression
https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql
So I would expect that your
CONVERT(DATETIME,:ls_datetime)
would evaluate to
CONVERT(DATETIME, 'ls_datetime')
but it should be
CONVERT(DATETIME, DateTimeColumn)
The error in your second statement could be that you're providing an wrong datetime format.
So please check if your error still occurs when you use this function
https://learn.microsoft.com/en-us/sql/t-sql/statements/set-dateformat-transact-sql
with the correct datetime format you're using

Advantage Database server crash

The following query makes an Advantage Database Server crash:
SELECT +SUM(0) FROM SYSTEM.IOTA
Note the + before SUM(0), this is the culprit.
If I replace + by -, no error occurs.
If I execute this query in local mode, this error pops up:
poQuery: Error 7200: AQE Error: State = S0000; NativeError = 2203;
[iAnywhere Solutions][Advantage SQL Engine]
An error was encountered while reading an intermediate or temporary file.
I discovered the problem while testing and, even if I agree there is no point to add a + before an aggregation instruction, it should not crash the server!
Thanks
It is confirmed to be a bug. The combination of the unary positive operator and the aggregate function is the cause of the problem. It will be fixed in a future update.