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");
Related
Say I call a stored procedure like this:
call SP_TEST('CAT','LION');
Now post successful run I get the query id using the command:
select last_query_id();
This returns the query id as 01a07606-0b02-362d-0001-1d6602361072
Say I want this query id to be written to a variable during runtime - such as :
create or replace procedure test_proc111
("SRC" VARCHAR(30), "TGT" VARCHAR(30))
returns varchar
language javascript
execute as owner
as '
{
var TABLE_VALUE = "";
var code1 = "select last_query_id();"
var code1_excecute = snowflake.execute({sqlText: code1});
while(code1_excecute.next()) {
var TABLE_VALUE = code1_excecute.getColumnValue(1);
}
return TABLE_VALUE
}
';
Now I call this stored procedure like this:
call test_proc1111('TEST','TARGET')
But I get this error:
Execution error in stored procedure TEST_PROC1111: Statement NULL not found At Snowflake. execute, line 8 position 31
How can we achieve this use case?
In the stored procedure in the question, you'll only get a query id if it's executed as caller:
create or replace procedure test_proc111
("SRC" VARCHAR(30), "TGT" VARCHAR(30))
returns varchar
language javascript
execute as caller
as '
var TABLE_VALUE;
var code1 = "select last_query_id(-1);"
var code1_excecute = snowflake.execute({sqlText: code1});
while(code1_excecute.next()){
TABLE_VALUE = code1_excecute.getColumnValue(1);
}
return TABLE_VALUE
'
;
This because if it's defined at execute as owner, it will only have a previous query id if there is another query executed within the procedure:
create or replace procedure test_proc111
("SRC" VARCHAR(30), "TGT" VARCHAR(30))
returns varchar
language javascript
execute as owner
as '
var TABLE_VALUE;
var code1 = "select last_query_id(-1);"
var code1_excecute = snowflake.execute({sqlText: code1});
var code1 = "select last_query_id(-1);"
var code1_excecute = snowflake.execute({sqlText: code1});
while(code1_excecute.next()){
TABLE_VALUE = code1_excecute.getColumnValue(1);
}
return TABLE_VALUE
'
;
I can't get a multiline SQL statement to work within a procedure/javascript without putting \ after each line.
This works:
CREATE or replace PROCEDURE PR_DELETEME()
RETURNS VARCHAR
LANGUAGE javascript
AS
$$
var rs = snowflake.execute( { sqlText:
'create or replace table deleteme as select sysdate() as my_date;'
} );
return 'Done.';
$$;
This fails:
CREATE or replace PROCEDURE PR_DELETEME()
RETURNS VARCHAR
LANGUAGE javascript
AS
$$
var rs = snowflake.execute( { sqlText:
'create or replace table deleteme as
select sysdate() as my_date;'
} );
return 'Done.';
$$;
call PR_DELETEME(); gives...
JavaScript compilation error: Uncaught SyntaxError: Invalid or unexpected token in PR_DELETEME at ' 'create or replace table deleteme as' position 6
I really don't want to have to put \ at the end of each line.
Use "backticks" not single quotes or double quotes. Below now works:
CREATE or replace PROCEDURE PR_DELETEME()
RETURNS VARCHAR
LANGUAGE javascript
AS
$$
var rs = snowflake.execute( { sqlText:
`create or replace table deleteme as
select sysdate() as sysd;`
} );
return 'Done.';
$$;
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")
}
};
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(?, ?)";
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
}