SQL Contains: Query operators - sql

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

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.

Unable to query using file in Data Proc Hive Operator

I am unable to query with .sql file in DataProcHiveOperator.
Though the documentation tells that we can query using file. Link of the documentation Here
It is working fine when I give query directly
Here is my sample code which is working fine with writing query directly:
HiveInsertingTable = DataProcHiveOperator(task_id='HiveInsertingTable',
gcp_conn_id='google_cloud_default',
query='CREATE TABLE TABLE_NAME(NAME STRING);',
cluster_name='cluster-name',
region='us-central1',
dag=dag)
Querying with file :
HiveInsertingTable = DataProcHiveOperator(task_id='HiveInsertingTable',
gcp_conn_id='google_cloud_default',
query='gs://us-central1-bucket/data/sample_hql.sql',
query_uri="gs://us-central1-bucket/data/sample_hql.sql
cluster_name='cluster-name',
region='us-central1',
dag=dag)
There is no error on sample_hql.sql script.
It is reading file location as a query and throwing me the error as:
Query: 'gs://bucketpath/filename.q'
Error occuring - cannot recognize input near 'gs' ':' '/'
Similar issue has also been raised Here
The issue is because you have passed query='gs://us-central1-bucket/data/sample_hql.sql' as well.
You should pass exactly 1 of query or queri_uri.
The code in your question has both of them, so remove query or use the following code:
HiveInsertingTable = DataProcHiveOperator(task_id='HiveInsertingTable',
gcp_conn_id='google_cloud_default',
query_uri="gs://us-central1-bucket/data/sample_hql.sql",
cluster_name='cluster-name',
region='us-central1',
dag=dag)

Translate JPQL query to plain SQL

I have a problem in my ejb java application .. and trying to solve it I stumble on this issue.
in my ejb class, I have a finder method
/* #ejb.finder signature="java.util.Collection findByAllMultiPlan(<list
of arguments>)"
*
* query="SELECT OBJECT(e)
* FROM ElementTbl as e
* WHERE <LOTS OF STUFF>"
That method fails sometimes, not all the time. (most of time when ran in a loop over a large data inputs) It can fail on a 8th, 10th or 35th iteration, etc..
It fails with the "Finder" exception
javax.ejb.EJBException: nested exception is: javax.ejb.FinderException:
Exception in findByCriteria while preparing or executing query:
statement: 'weblogic.jdbc.wrapper.PreparedStatement_com_inet_tds_r#bd44a1'
java.sql.SQLException: The transaction is no longer active - status:
'Rolling Back. [Reason=weblogic.transaction.internal.TimedOutException:
Transaction timed out after 33 seconds
BEA1-655E9CABEFC3D5D9F078]'. No further JDBC access is allowed within this
transaction.java.sql.SQLException: The transaction is no longer active -
status: 'Rolling Back.
[Reason=weblogic.transaction.internal.TimedOutException: Transaction timed
out after 33 seconds BEA1-655E9CABEFC3D5D9F078]'. No further JDBC access is
allowed within this transaction.
In a log files, I see query executed in weblogic jpql format
SELECT WL0.mreUidID, WL0.mreDtmCreated, WL0.mreCdeCustType, WL0.mreIn...
.. many,many fields ...
FROM tblElement WL0 WHERE ( ( ( ( ( ( ( ( ( WL0.elVchID= ? ) AND (
((((((((WL0.elVchAnotherId = ? ) .. and a lot of fields encoded with
"WL0.field =?"
There is NO output of values that are set in place of ?
So, question is: Is it possible to translate that JPQL language into plain SQL so to run in query analyzer?
Again, query is very large to be parsing it every time I need to test a particular scenario... it will take me close to more than an hour to fill in everything..
There must be a java method or utility to get access to this from within my EJB class.
Please help !!!

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