"SELECT ##IDENTITY" throws overflow error - sql

I am working on an MS Access solution where I need to insert a row and use its ID from within VBA. Thankfully, SQL gave us ##IDENTITY for exactly this purpose.
Or so I thought. In practice, it throws an overflow error. What can be possible causes of such an error?
Multiple Google searches could not come up with a solution.
Full line of code:
iid = CurrentDb.OpenRecordset("SELECT ##IDENTITY")(0)
German error message:
Laufzeitfehler '6': Überlauf
Translation:
Runtime Error '6': Overflow

Perhaps you need to declare iid as a 'variant' instead of an 'integer'?
http://www.dbforums.com/microsoft-access/1666779-any-idea-why-i-get-runtime-error-6-overflow.html

Related

Why am I getting antlr.MismatchedTokenException thrown for simple SQL query parsing (.NET)?

Queries are read fine when debugging only My Code. However, when disabling the option in Visual Studio in 'Debug > Options > Enable Just My Code', antlr.MismatchedTokenException is thrown for almost every SQL query which is run through the parser.
Here's a simplified example, which throws:
antlr.MismatchedTokenException 'expecting "Dot", found 'AS''
Ignoring the exceptions, the select statement works fine and is passed to the app no problem. But the constant spam of .NET exceptions is puzzling. The literal interpretation of the exception doesn't seem to make sense for this SQL query.
I can't see anything wrong with this example query.
private static StatementList PrepareSql(string sql)
{
sql = "SELECT A.[ID] AS [Activities_Id] FROM [Activities] A";
var parser = new TSql140Parser(false, SqlEngineType.SqlAzure);
return parser.ParseStatementList(new StringReader(sql), out var errors1);
}
--
Update:
We believe the exception occurs not in the immediate library referenced by the portal, but in ANTLR. We still cannot find the reason for the exception. Currently we've added a workaround to cache the SQL query so that the exception is only thrown once per query - but that's still one too many.

[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

SQL Contains: Query operators

I got a query working from long ago, the query is build by JPA, but today threw an exception:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.orm.hibernate3.HibernateJdbcException: JDBC exception on Hibernate data access: SQLException for SQL [n/a]; SQL state [99999]; error code [29902]; could not extract ResultSet; nested exception is org.hibernate.exception.GenericJDBCException: could not extract ResultSet at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
Long story short, it gives SQL state [99999]; error code [29902]; a parameter problem.
The query its build like this:
select * from mytable where CONTAINS(field1, 'BT');
A little google-fu show me that BT its a query operator for Contains. This error can be reproduced searching by NOT, DIFMERGE, ABOUT, AND..
The question is, there is a way to escape the text so i can search by the word BT or his friends?
EDIT: i build the contains like this:
return builder.greaterThan(
builder.function("CONTAINS", Integer.class, exp, builder.literal(((String) param.getValue()).toUpperCase())),
0);
EDIT: I tried converting the text BT to unicode, but doesn't work either.
select * from myTableWHERE CONTAINS(field1, UNISTR('\0042\0054')) > 0

Can not catch error in merge statement

Hi I have problem to catch the error in merge statement.
The merge statement will get the following error when run in SQL Plus:
ORA-00932: inconsistent datatypes: expected DATE got NUMBER
When I execute the statement with ExecuteNonQuery in Vb.net will not get any exception.
Is there any way to catch that error in Vb.net without using log errors in merge statement?
EDIT:
Try
Dim count as Interger=0
Dim cmd As New OleDb.OleDbCommand(strSQL)
count = cmd .ExecuteNonQuery()
Catch ex as Exception
... Respond ...
End Try
Sample Merge Statement with error:
MERGE INTO TABLE_A a USING
(
SELECT ID,SUM(AMOUNT) AMOUNT
FROM TABLE_B b
GROUP BY ID
) b
ON ( b.ID=a.ID )
WHEN MATCHED THEN
UPDATE SET a.AMOUNT=a.AMOUNT+b.AMOUNT
WHEN NOT MATCHED THEN
INSERT (ID, DT, AMOUNT) VALUES (b.ID, 0, b.AMOUNT);
The SQL Plus will get error at the insert but Vb.net can not catch.
The data type for field DT is DATE.
EDIT 3:
My bad - in that case, I think you'll have to subscribe to oleDbConnection.InfoMessage event and check for applicable SQL-00932 messages after the MERGE command. I don't see any equivalent property to FireInfoMessageEventOnUserErrors in OLE-DB (which, again, seems totally backwards...).
EDIT 2:
It appears there's a default threshold that will cause vb.net to ignore certain SQL exceptions (can't fathom the rationale behind such a decision). Regardless, I think if you do the following, you should see the exception raised.
cmd.FireInfoMessageEventOnUserErrors = true;
End EDIT2
EDIT: I just read the problem a bit closer. If the vb.net execution is not throwing any exception at all, that would indicate that you're not running the exact same statement in both. My initial guess is that you're passing a date parameter that is passing SQL Plus string variable incorrectly as 01/01/2015 (which is 1 divided by 1 divided by 2015) and vb.net variable correctly as '01/01/2015' (that is, as a string with quotation marks).
End EDIT
I have no idea what you mean by "using log errors in merge statement", but it sounds like you just need the syntax for using the vb.net try-catch syntax to catch and act on Oracle errors.
From here: http://www.tek-tips.com/viewthread.cfm?qid=467119
Pertinent code snippet:
Try
... Sql here ...
Catch ex as OleDbException
... Respond to error here ...
End Try

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