简体   繁体   中英

How to get return value from stored procedure with output parameter in Grails?

I have a stored procedure that has an output parameter which is a cursor

Here's the code for the stored procedure I currently use, I don't know how to get the return value

def kf_fpy
sql.call("{call calfpy(?,?,?,?,?)}",[workcenter,product,stattime,endtime,Sql.resultSet(OracleTypes.CURSOR)])

{
                cursorResults ->
                   cursorResults.eachRow() {
                        x ->
                          kf_fpy = x.getAt('kf_fpy')//ERROR OCCURED
                   }
}

And I got an error "Method threw 'java.lang.NullPointerException' exception. Cannot evaluate $Proxy11.toString()"

Stored Procedure:

PROCEDURE CALFPY(workcenter in varchar2,produ in varchar2,stdt in varchar2,etdt in varchar2,p_out out pkg_package.type_cursor)

PKG_PACKAGE:

create or replace
package pkg_package 
as 
type type_cursor is ref cursor; 
type type_record is record 
( 
kf_ws varchar2(20), 
kf_fpy number,
kf_tfpy number,
kf_pro varchar2(200)

); 
end;

enter image description here

Check out this solution :

Procedure :

create or replace procedure GRAILS_EXAMPLE
(
  v_name IN VARCHAR2,
  ref_cur1 IN OUT SYS_REFCURSOR
)
AS
begin

  OPEN ref_cur1 FOR
  SELECT UPPER(v_name) AS UPPER_NAME
  FROM DUAL;

end GRAILS_EXAMPLE;

Controller which calls procedure :

    String upperName;
    sql.call("BEGIN GRAILS_EXAMPLE(?,?); END;",
            [params.name, Sql.resultSet(OracleTypes.CURSOR)]) {cursorResults ->

            if (cursorResults.next()) {
                 upperName = cursorResults.getAt('UPPER_NAME');
            }

    }

If you need to get at a field that isn't properly aliased in your proc you can still access it by its index. For example the follow would also work in the above controller:

 upperName = cursorResults.getAt(0) 

If you need more values from procedure check out this answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM