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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With