Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning value from PL/SQL block

I need to return a value from a PL/SQL block in MyBatis.

The PL/SQL block is this:

DECLARE
BEGIN
    <foreach collection="params" item="param">
        UPDATE PRIORITIES
        SET PRIORITY = #{param.priority}
        WHERE ID = #{param.id};
    </foreach>
END

The reason I need to return a value, is that I want to be able to get the number of affected rows.

I know I could do it like this:

<update id="updateAll" parameterType="map">
    { call
    DECLARE
    BEGIN
        <foreach collection="params" item="param">
            UPDATE PRIORITIES
            SET PRIORITY = #{param.priority}
            WHERE ID = #{param.id};
        </foreach>

        #{affected_rows, jdbcType=DECIMAL, mode=OUT} := sql%rowcount;
    END
    }
</update>

But this means I must have a method like this in the java mapper:

public void updateAll(Map<String, String> parameters);

I have this instead:

public int updateAll(@Param("params") List<PriorityModel> model);

Is there a way to return that value without having a map?

like image 594
BackSlash Avatar asked Dec 23 '14 10:12

BackSlash


People also ask

How do you return a value in PL SQL?

After the stored procedure call, the variables will be populated with return values. If you want to have RETURN value as return from the PL/SQL call, then use FUNCTION . Please note that in case, you would be able to return only one variable as return variable.

Can a procedure return a value in Plsql?

A Procedure in SQL can have a RETURN statement to return the control to the calling block, but it cannot return any values through the RETURN statement. Procedures cannot be called directly from SELECT statements. They can be called from another block or through EXEC keyword.

Can we return a value from procedure in Oracle?

A stored procedure does not have a return value but can optionally take input, output, or input-output parameters. A stored procedure can return output through any output or input-output parameter.

Can PL SQL function return more than one value?

There is no way you can return 2 variable. It has to be one. You can use a custom rec type or array which you can return from the function.


1 Answers

I think you can't return a value from a procedure call. It's possible set a value in an object like a pojo or a map like your example but, i think if you at the same sql transaction execute a query after the updates like this:

SELECT sql%rowcount FROM DUAL;

You can get the number of affected rows like a ResultSet.

like image 144
maframaran Avatar answered Oct 21 '22 05:10

maframaran