Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a stored procedure (not function) with INOUT parameter in PostgreSQL 13

I have this stored procedure :

CREATE OR REPLACE PROCEDURE SP_ObtenerSecuencialFactura(INOUT p_secuencial INT)
LANGUAGE PLPGSQL 
AS 
$$
BEGIN
    SELECT MAX("CODIGOFACTURA") + 1 INTO p_secuencial FROM "FACTURA";
    IF p_secuencial IS NULL THEN
        p_secuencial := 1;
    END IF;
END
$$

And the calling:

DECLARE secuencial INT;
CALL SP_ObtenerSecuencialFactura(secuencial);
RAISE NOTICE '%', secuencial;

But I get this error when I call that stored procedure:

ERROR: syntax error at or near "INT"

LINE 1: DECLARE secuencial INT;

What's wrong? I was finding examples but only exist with functions.

like image 682
Ejrr1085 Avatar asked Nov 07 '22 04:11

Ejrr1085


1 Answers

This is the solution:

DO
$$
DECLARE secuencial INT;
BEGIN
    CALL SP_ObtenerSecuencialFactura(secuencial);
    RAISE NOTICE '%', secuencial;
END
$$

NOTICE: 1

DO

Query returned successfully in 85 msec.

PostgreSQL use PL/pgSQL like Oracle with PL/SQL, so, to call a Store Procedure with OUTIN parameter, we need envolved the calling and the variable in Anonymous Block with "do" and "$$"

DO in PostgreSQL

like image 139
Ejrr1085 Avatar answered Dec 02 '22 12:12

Ejrr1085