Calling stored procedure from groovy using GString variation - sql

In groovy tutorial: http://groovy.codehaus.org/Database+features
there is section about procedure. When I try this example:
The same example again but with a GString variation:
def first = 'Sam'
sql.call("{$Sql.VARCHAR = call FullName($first)}") { name ->
assert name == 'Sam Pullara'
}
I got exception:
Chyba: ORA-06550: line 1, column 13:
PLS-00222: no function with name 'FULLNAME' exists in this scope
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
java.sql.SQLException: ORA-06550: line 1, column 13:
PLS-00222: no function with name 'FULLNAME' exists in this scope
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:113)
yes that is true what exception says because I got just procedure FULLNAME what I want to call not function. Is this tutorial still actual ?

In Oracle, a stored procedure cannot return a value and a function can return a value. This is why you get that error. You can verify using my testing stored procedure and groovy code...
CREATE OR REPLACE PROCEDURE test_procedure
( string_in IN OUT VARCHAR2 ) IS
BEGIN
string_in := 'hi ' || string_in ;
END;
import groovy.sql.Sql
Sql sql = Sql.newInstance("jdbc:oracle:thin:#hostname:1521:dbname", username","password","oracle.jdbc.driver.OracleDriver")
def first="Wade"
sql.call '{call test_procedure(?)}',
[Sql.inout(Sql.VARCHAR(first))],
{ test ->
println ">>" + test + "<<"; // >>hi Wade<<
}

import groovy.sql.Sql
import oracle.jdbc.driver.OracleTypes
Sql sql = Sql.newInstance("jdbc:oracle:thin:#hostname:1521:dbname", "username","password","oracle.jdbc.driver.OracleDriver")
dept_id = 50
sql.call('{? = call FullName(?)}', [Sql.VARCHAR, 'Sam']) { name->
println name
}

Related

ORA-01036: illegal variable name/number while running PL SQL block from nodejs

I have following pl/sql block:
DECLARE
user_name varchar(255);
custom_exception EXCEPTION;
PRAGMA exception_init( custom_exception, -20001);
BEGIN
SELECT name
INTO user_name
FROM data_table where ID = '1';
IF user_name = 'temp' THEN
RAISE custom_exception;
END IF;
END;
When I run it from Oracle SQL Developer, it works fine.
But I am getting ORA-01036: illegal variable name/number while running it from nodejs.
Call from Nodejs code:
var output = await connection.execute(fs.readFileSync('path_to_pl_sql_file', 'utf8'), {
message: {
dir: oracledb.BIND_OUT,
type: oracledb.STRING,
maxSize: 100
}
}, {
autoCommit: true
});
Can someone point out what going wrong here?
The error was not with the PL/SQL block.
I was passing in bind variables while executing the PL/SQL from nodejs and that bind variable was not present in the SQL.
Hence the error!

Spring Data Jpa call to Stored Procedure

I am trying to create a method that call Stored Procedure to do a very easy insert and a select,
I created a stored procedure on an Oracle database as below:
CREATE OR REPLACE PROCEDURE schema.insertNewTable(p_test IN VARCHAR2, p_out OUT VARCHAR2)
IS
BEGIN
INSERT INTO NEWTABLE("TEST") VALUES(p_test);
SELECT '1' INTO p_out FROM DUAL;
COMMIT;
END;
Then on an Entity:
#Procedure(procedureName = "insertNewTable", outputParameterName="p_out")
String insertNewTable(#Param("p_test") String p_test);
but everytime i call this method i get this:
2018-06-23 20:04:43,047|ORA-06550: row 1, colonna 7:
PLS-00306: wrong number or types of arguments in call to 'INSERTNEWTABLE'
ORA-06550: riga 1, column 7:
PL/SQL: Statement ignored
2018-06-23 20:04:43,100|org.springframework.dao.InvalidDataAccessResourceUsageException: Error calling CallableStatement.getMoreResults; SQL [insertNewTable]; nested exception is org.hibernate.exception.SQLGrammarException: Error calling CallableStatement.getMoreResults
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:261)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:244)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:503)
After a couple of tries (and exceptions), I modified the Entity and the call to avoid "OUT" types and, in this way, I have no errors:
CREATE OR REPLACE PROCEDURE schema.insertNewTable(p_test IN VARCHAR2)
IS
BEGIN
INSERT INTO NEWTABLE("TEST") VALUES(p_test);
COMMIT;
END;
Then on an Entity:
#Procedure(procedureName = "insertNewTable")
void insertNewTable(#Param("p_test") String p_test);
How can i fix this to get a return value?
with Spring DATA
in your repository:
1) calling with native SQL :
#Query(value = "select Store-procedure-name(:param) from dual", nativeQuery = true)
BigDecimal method-name(#Param("param") Long param);
2)
#Procedure(procedureName = "store-procedure-name", outputParameterName = "output-param-of-store-procedure")
return-type-of-store-procedure method-name(input-params-of-store-procedure);
you can use NamedStoredProcedureQuery
#NamedStoredProcedureQuery(
name = "insertNewTable",
procedureName = "insertNewTable",
parameters = {
#StoredProcedureParameter(mode = ParameterMode.IN, type = String.class, name
= "p_test"),
#StoredProcedureParameter(mode = ParameterMode.OUT, type = String.class, name
= "p_out"),
)
}
)
method call:
StoredProcedureQuery query =
this.em.createNamedStoredProcedureQuery("insertNewTable");
query.setParameter("p_test", ""TEST"");
query.execute();
String out = query.getOutputParameterValue("p_out");

How to get cursor from Oracle using Groovy?

I'm using a Groovy script in Mule ESB to get output parameters from Oracle stored procedure (including cursor) and getting an exception.
Minimal example:
import groovy.sql.Sql
import oracle.jdbc.pool.OracleDataSource
import oracle.jdbc.driver.OracleTypes
def ds = new OracleDataSource()
// setting data source parameters here
def sql = new Sql(ds)
def data = []
sql.call("""declare
result_table sys_refcursor;
begin
open result_table for select 1 as a from dual;
insert into CURSOR_TEST (ID) values (1);
commit;
${Sql.resultSet OracleTypes.CURSOR} := result_table;
insert into CURSOR_TEST (ID) values (2);
commit;
end;
"""
){ table ->
throw new RuntimeException("Never getting this exception.")
table.eachRow {
data << it.toRowResult()
}
}
sql.close()
return data
Error:
Message : java.sql.SQLException: Closed Statement (javax.script.ScriptException)
Code : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. Closed Statement(SQL Code: 17009, SQL State: + 99999) (java.sql.SQLException)
oracle.jdbc.driver.SQLStateMapping:70 (null)
2. java.sql.SQLException: Closed Statement (javax.script.ScriptException)
org.codehaus.groovy.jsr223.GroovyScriptEngineImpl:323 (http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/script/ScriptException.html)
3. java.sql.SQLException: Closed Statement (javax.script.ScriptException)
(org.mule.api.transformer.TransformerException)
org.mule.module.scripting.transformer.ScriptTransformer:39 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/transformer/TransformerException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
java.sql.SQLException: Closed Statement
at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:199)
+ 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************
Select from CURSOR_TEST returns 1 and 2.
Oracle server version: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production.
Mule version: 3.5.0.
I'm using jdbc\lib\ojdbc6.jar from oracle client version 11.1.0.7.0.
What am I doing wrong?
The following code can help you get variable of SYS_REFCURSOR from Oracle anonymous block.
We should focus on a few key details:
Class groovy.sql.Sql doesn't have corresponding OutParameter and we make it manually as CURSOR_PARAMETER and pass it to sql.call method
Consider that the block starts with {call DECLARE and ends with END } without semicolon after END. Otherwise we can get a poorly recognizable SQLException in the face.
The question marks ? inside the sqlString are places for parameter bindings. Bindings are made in the natural order. In this example:
the first ? binds with the first element in parametersList: "abc", treating the value as IN parameter ;
the second ? binds with CURSOR_PARAMETER treating the value as OUT parameter of passed type;
There is only one enter into closure after sql.call and ResultSet rs provide rows of cursor my_cur declared in anonymous block.
import groovy.sql.OutParameter
import groovy.sql.Sql
import oracle.jdbc.OracleTypes
import java.sql.ResultSet
def driver = 'oracle.jdbc.driver.OracleDriver'
def sql = Sql.newInstance('jdbc:oracle:thin:#MY-SERVER:1521:XXX', 'usr', 'psw', driver)
// special OutParameter for cursor type
OutParameter CURSOR_PARAMETER = new OutParameter() {
public int getType() {
return OracleTypes.CURSOR;
}
};
// look at some ceremonial wrappers around anonymous block
String sqlString = """{call
DECLARE
my_cur SYS_REFCURSOR;
x VARCHAR2(32767) := ?;
BEGIN
OPEN my_cur
FOR
SELECT x || level AS my_column FROM dual CONNECT BY level < 10;
? := my_cur;
END
}
""";
// the order of elements matches the order of bindings
def parametersList = ["abc", CURSOR_PARAMETER];
// rs contains the result set of cursor my_cur
sql.call(sqlString, parametersList) { ResultSet rs ->
while (rs.next()) {
println rs.getString("my_column")
}
};

Can not use method void java.sql.CallableStatement.setObject(String parameterName, Object x, int targetSqlType)

This is my Procedure code:
Specification:
create or replace PACKAGE PKG_NALD AS
PROCEDURE PROC_KETQUA(res OUT SYS_REFCURSOR ,maMH STRING);
END PKG_NALD;
Body:
create or replace PACKAGE BODY PKG_NALD AS
PROCEDURE PROC_KETQUA(res OUT SYS_REFCURSOR ,maMH STRING) AS
BEGIN
OPEN res FOR
select MA_MH as maMH123, TEN_MH as tenMH, TIN_CHI as tinChi, KHOA as khoa from nald_mon_hoc where upper(ma_mh) = upper(maMH);
END PROC_KETQUA;
END PKG_NALD;
And this's my code in order to call Procedure through CallableStatement:
String sqlQuery = "call PKG_NALD.PROC_KETQUA(?, :maMH)";
Connection connection = ((SessionImpl)session).connection();
CallableStatement callable = connection.prepareCall(sqlQuery);
callable.setObject (2, "COSC1310", Types.VARCHAR); // This method belongs to java.sql.PreparedStatement
callable.registerOutParameter(1, OracleTypes.CURSOR);
callable.execute();
The program can run correctly. However, if I replace callable.setObject (2, "COSC1310", Types.VARCHAR) with
callable.setObject("maMH", "COSC1310", Types.VARCHAR); // This method belongs to java.sql.CallableStatement
I received the error:
Exception in thread "main" java.sql.SQLException: The number of parameter names does not match the number of registered praremeters
at oracle.jdbc.driver.OracleSql.setNamedParameters(OracleSql.java:196)
at oracle.jdbc.driver.OracleCallableStatement.execute(OracleCallableStatement.java:4708)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.execute(OraclePreparedStatementWrapper.java:1374)
at com.mkyong.util.QueryUtil.getListFromPackage(QueryUtil.java:71)
at com.mkyong.App.main(App.java:35)
Despite of the document here, something went wrong. What should I do ?
Try changing this
String sqlQuery = "call PKG_NALD.PROC_KETQUA(?, :maMH)";
To this
String sqlQuery = "call PKG_NALD.PROC_KETQUA(?, ?)";

How can I call a stored procedure using Groovy?

How can I call a stored procedure using Groovy?
How can I create a stored procedure from Grails project (as domain classes to create data base)?
An example of calling a FullName stored procedure which takes a param ('Sam' in the example) and returns a VARCHAR.
sql.call("{? = call FullName(?)}", [Sql.VARCHAR, 'Sam']) { name ->
assert name == 'Sam Pullara'
}
The same example again but with a GString variation:
def first = 'Sam'
sql.call("{$Sql.VARCHAR = call FullName($first)}") { name ->
assert name == 'Sam Pullara'
}
Here is an example of a stored procedure with an out parameter:
sql.call '{call Hemisphere(?, ?, ?)}', ['Guillaume', 'Laforge', Sql.VARCHAR], { dwells ->
println dwells // => Northern Hemisphere
}
Refer this.