SimpleJdbcCall : No function exist error - spring

I am getting Exception while executing store procedure. Exception is as below
org.springframework.jdbc.BadSqlGrammarException: CallableStatementCallback;badSQLgrammar [{call find_spot()}]; nestedexception is org.postgresql.util.PSQLException: ERROR: functionfind_spot()
does not exist Hint: No function matches the given name and argument types. You might need to add explicit type casts.Position:15
Its saying function find_spot() does not exist but I checked in database this procedure is there. I am using Postgresql [DBeaver]
Can anyone help me to solve this?

You should cast a json to make search or use the json access query, see the examples:
select * from table where cast(field_json as varchar(500)) !~ 'reg_ex' and id = 11
or
select field_json->>'key' from table where field_json->>'key' ilike 'value'

Related

Unable to check the dataflow output activity metrics - ADF

I am trying to check if the dataflow has written any rows to the sink and capture the activity output. the update statement fails, if the activity doesn't write any rows to the sink , so as per MS docs I am trying the below expression in a lookup activity.
***DECLARE #Date DATETIME;
SET #Date = GETDATE();
DECLARE #ROWSAFFECT INT;
SET #ROWSAFFECT = if(contains(#{activity('dataflow').output.runStatus}, 'sink'), '#{activity('dataflow').output.runStatus.metrics.sink.rowsWritten}','0');
update table [schema].[audit_table]
SET LOAD_STATUS ='Success'
,ROWS_AFFECTED = #ROWSAFFECT
select 1;***
But this fails with a parse error. Can someone please help me with this?
=A database operation failed with the following error: 'Parse error at line: 4, column: 217: Incorrect syntax near ']'.'
The query written with dynamic content to assign the value for #ROWAFFECT is incorrect.
The main issue is with the #contains() inside the if. You should be searching for 'sink' in contains(activity('dataflow').output.runStatus.metrics.
I have tried the following query to insert into my azure SQL database table (dbo.audit) for demonstration.
DECLARE #ROWSAFFECT INT;
SET #ROWSAFFECT = #{if(contains(activity('dataflow').output.runStatus.metrics, 'sink'), activity('dataflow').output.runStatus.metrics.sink.rowsWritten,0)};
update [dbo].[audit] SET LOAD_STATUS ='Success' ,ROWS_AFFECTED = #ROWSAFFECT
The following is the debug query that is being run for each case.
When rowsWritten is 0:
When there are n=10 records:

How to use Oracle query hint in Hibernate

I am trying to use Oracle hint in Hibernate to call force index, but didn't find any proper API in Hibernate 3.6.10.Final.
I somehow tried with projections in Hibernate criteria:
proList.add(Projections.sqlProjection("/*+ INDEX_DESC(CONTACT_USER_FK_I) */", new String[]{}, new Type[]{}));
proList.add(Projections.property("objectId"));
criteria.setProjection(proList);
return criteria.list();
But I am getting the exception below:
EXCEPTION
Caused by: 8 SQL Error (could not execute query; SQL [select /*+ INDEX_DESC(CONTACT_USER_FK_I) */, this_.CONTACT_ID as y0_ from R4GDEV01_MBW.CONTACT this_ w
here this_.USER_ID=? and this_.ADDRESS_BOOK_ID in (?) and this_.DELETION_DATE is null order by lower(this_.FIRSTNAME) asc]; nested exception is org.hibernate
.exception.SQLGrammarException: could not execute query)
at com.fusionone.pml.dao.hibernate.AbstractDao.executeCallback(AbstractDao.java:391)
at com.fusionone.pml.dao.hibernate.AbstractContactDao.searchContacts(AbstractContactDao.java:1019)
at com.fusionone.nab.core.service.impl.MergeServiceImpl.getFilteredContactIds(MergeServiceImpl.java:154)
... 91 more
I found out that the projection is appending a , after query hint.
Is there any other way to use Oracle query hint in Hibernate 3.6.10 criteria or in HQL?
Thanks
You should try to search first. Here is a simple trick to solve this, just transform the hint into a column:
"/*+ INDEX_DESC(CONTACT_USER_FK_I) */ 1 as MYHINT"
Your problem is that there is nothing previous to ,. Due to you want to add a projection on objectIdattribute, you could transform your criteria to somenthing like this:
Projections.sqlProjection("/*+ INDEX_DESC(CONTACT_USER_FK_I) */ objectId", new String[]{}, new Type[]{}))

Binding on `statement` not supported in Eloquent 5.1?

I'm new to Laravel and trying to do a string query in Eloquent. I was trying to use DB::statement, but I kept getting errors about placeholders in the query string. It seems I either don't have the syntax right, or bindings are unimplemented or unsupported?
The reason I want to use statement is because I'm doing an INSERT... SELECT, which I haven't been able to find any documentation about in Eloquent.
Here's my code:
$ php artisan tinker
Psy Shell v0.5.2 (PHP 5.6.13-0+deb8u1 — cli) by Justin Hileman
>>> echo \DB::statement('CREATE DATABASE :db', [':db'=>'test']);
Illuminate\Database\QueryException with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1 (SQL: CREATE DATABASE :db)'
>>> \DB::statement('CREATE DATABASE ?', ['test']);
Illuminate\Database\QueryException with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1 (SQL: CREATE DATABASE test)'
These are the two syntax forms (? and :string) from PDO. Other methods in DB such as select and insert support this, according to the documentation.
The relevant parts of these errors are near '?' at line 1 (SQL: CREATE DATABASE :db) and near '?' at line 1 (SQL: CREATE DATABASE test). MySQL thinks there is an unbound ? in the query string. I didn't even use that syntax in the first query. I'm concluding from that that the bind() method did not correctly bind my placeholders.
This question on Laracasts is asking about the syntax, but there is no accepted answer.
Edit One answer says that statement() doesn't support CREATE. I tried some queries out with SELECT, and got the same results, with both placeholders:
>>> \DB::statement('SELECT 1 WHERE \'a\' = ?', array('a'));
Illuminate\Database\QueryException with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE 'a' = ?' at line 1 (SQL: SELECT 1 WHERE 'a' = a)'
>>> \DB::statement('SELECT 1 WHERE \'a\' = :letter', array(':letter'=>'a'));
Illuminate\Database\QueryException with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE 'a' = ?' at line 1 (SQL: SELECT 1 WHERE 'a' = :letter)'
Actually, you can use create and drop query in DB::statement(), but named bindings is not used in that way.
Here are some queries that will success.
drop and create do not accept bindings.
>>> \Db::statement('create database test')
=> true
>>> \Db::statement('drop database test')
=> true
Do not use backslash and single quotes in the statement
>>> \Db::statement('insert into users (id, name) values (?, ?)', ['1', 'John'])
=> true
DB::statement() only return ture when success, so if you want to see select results, you should use DB::select()
>>> \Db::statement('select * from users')
=> true
>>> \Db::select('select * from users')
=> [
{#770
+"id": 1,
+"name": "John",
},
]
Remove leading : in the second argument.
>>> \Db::statement('update users set name = :name where id = :id', ['id' => 1, 'name' => 'John'])
=> true
You will get affect rows if you use DB::update and DB::delete
>>> \Db::delete('delete from users where id = :id', ['id' => 1])
=> 1
The errors you receive are only indirectly related with Laravels DB::statement() function. They all fail within that method at the line
return $me->getPdo()->prepare($query)->execute($bindings);
within the file vendor/laravel/framework/src/Illuminate/Database/Connection.php
Responsible for that failure is the resulting call to PDO::prepare()
The Docuemenation says:
Parameter markers can represent a complete data literal only. Neither part of literal, nor keyword, nor identifier, nor whatever arbitrary query part can be bound using parameters. For example, you cannot bind multiple values to a single parameter in the IN() clause of an SQL statement.
Also have a look at the user contributed notes at the above php.net documentation. Additionally have a look at Can PHP PDO Statements accept the table or column name as parameter?
Your create examples are not supported by PDO.
The reason your SELECT examples fail is simply due to an invalid syntax.
\DB::statement('SELECT 1 WHERE \'a\' = ?', array('a'))
You are simply missing the FROM clause. This example works perfeclty well at my test computer:
$ret = \DB::statement('SELECT 1 FROM `users` WHERE `username` = ?', ["gregor"]);
But
$ret = \DB::statement('SELECT 1 WHERE `username` = ?', ["testname"]);
Generates the exact error, you receive.
Also note, that \DB::statement does not return any ressources. It just indicates by returning true or false, whether the query suceeded.
Your option is to use DB::raw() within your insert() statement, if you want to use INSERT...SELECT. Some googling will help you, to find the proper solution. Maybe as Starting Points: Raw Queries in Laravel, or How to use raw queries in Laravel
What you're trying to do is passing the table name through binding.
DB::statement('select * from ?',['users'])
which according to this post, it's not possible.
of course if you want to sanitize the data you can use an array of short codes like so:
$tables = ['users','test','another'];
and the query would look something like:
$code = 0;
DB::statement("select * from $tables[$code] where a=?",[2]);
DB::statement("create table $tables[$code]");

Why do I get "ORA-00932: inconsistent datatypes: expected - got -" when using COLLECT() in a prepared statement?

I am using this query with the Perl DBI:
SELECT c.change_id
, COLLECT(t.tag) AS the_tags
FROM changes c
LEFT JOIN tags t ON c.change_id = t.change_id
WHERE c.project = ?
GROUP BY c.change_id
The DBI uses OCI to prepare this statement, bind the value I pass, and get the results. But Oracle, for some reason, does not like it. The error output is:
ORA-00932: inconsistent datatypes: expected - got - (DBD ERROR: error possibly near <*> indicator at char 41 in '
SELECT c.change_id
, <*>COLLECT(t.tag) AS the_tags
FROM changes c
LEFT JOIN tags t ON c.change_id = t.change_id
WHERE c.project = :p1
GROUP BY c.change_id
'
Not very informative. However, I can make this error go away not only by changing the call to COLLECT() also by replacing the placeholder with the actual value:
SELECT c.change_id
, COLLECT(t.tag) AS the_tags
FROM changes c
LEFT JOIN tags t ON c.change_id = t.change_id
WHERE c.project = 'tryoracle'
GROUP BY c.change_id
That version works perfectly. Why doesn't Oracle like the prepared statement with the COLLECT()?
In case it's any help, here is a trace of the OCI-related calls extracted via ora_verbose = 6 (h/t #bohica).
Finally got a solution to this issue, thanks to some digging by a user. The problem was not with the placeholder; why it worked without the placeholder on the VirtualBox image I have no idea. No, the issue was with the COLLECT(). Seems that both the values being collected need to be cast to a specific type, and the resulting array also needs to be cast to a pre-defined array data type. Just so happens that my code has a custom array type:
CREATE TYPE sqitch_array AS varray(1024) OF VARCHAR2(512);
So I'm able to get the query to work by casting the COLLECT() like so:
CAST(COLLECT(CAST(t.tags as VARCHAR2(512))) AS sqitch_array)

Oracle XMLDB's XMLCAST and XMLQUERY incompatible with iBatis?

I've been trying to select a list of values from XMLs stored in an
XMLType column but I keep getting the errors which are listed at the
tail end of this post.
The select id is
getXMLFragment
, and the relevant subset of the
sqlmap.xml is as follows:
<select id="getXMLFragment" resultClass="list">
SELECT
XMLCAST(XMLQUERY('$CUSTOMER/CUSTOMER/DETAILS/
CUST_NAME/text()' PASSING CUSTOMER AS
"CUSTOMER" RETURNING CONTENT) AS VARCHAR2(20))
AS customers FROM SHOP.CLIENT_INFO
</select>
(CUSTOMER is an XMLType column in CLIENT_INFO)
and I call the statement using
List<String> custNames= (List<String>)
sqlMap.queryForList("getXMLFragment");
I am using ibatis-2.3.4.726.jar.
Is it because iBatis does not recognise XMLDB queries and hence,
tokenizes the string wrongly? On a sidenote, I have implemented
XMLTypeCallback.java to handle XMLType insertions successfully, and I
think it will work should I wish to retrieve the entire XML. However,
in this case, I need to extract only individual values due to
requirements. A workaround would be greatly appreciated.
Thanks in advance.
The exceptions generated are listed below:
--- The error occurred in sqlMap.xml.
--- The error occurred while preparing the mapped statement for
execution.
--- Check the getXMLFragment.
--- Check the SQL statement.
--- Cause: java.util.NoSuchElementException
at
com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryWithCallback(MappedStatement.java:
204)
at
com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryForList(MappedStatement.java:
139)
at
com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:
567)
at
com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:
541)
at
com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForList(SqlMapSessionImpl.java:
118)
at
com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForList(SqlMapSessionImpl.java:
122)
at
com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.queryForList(SqlMapClientImpl.java:
98)
at Main.main(Main.java:60)
Caused by: java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:332)
at
com.ibatis.sqlmap.engine.mapping.sql.simple.SimpleDynamicSql.processDynamicElements(SimpleDynamicSql.java:
90)
at
com.ibatis.sqlmap.engine.mapping.sql.simple.SimpleDynamicSql.getSql(SimpleDynamicSql.java:
45)
at
com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryWithCallback(MappedStatement.java:
184)
... 7 more
Update: We just need to repeat '$' once such that the sql is "....XMLQUERY('$$CUSTOMER....." The tokenization works fine after that and the query executes successfully.

Resources