Stored Procedures

About

Stored procedures do not return values with RETURN, so the use of RETURN is only to immediatly stop the procedure. (check Stored Procedures)

circle-check
circle-info

Procedures tend to be more performant than functions, because they are compiled objects.

The code is compiled only when the procedure is created (or changed).

Creating

The syntax is very similar to Functions.

circle-info

You can use pr prefix on procedure names when creating them.

CREATE OR REPLACE PROCEDURE procedure_name(p1 type, ...) AS
$$
    DECLARE
        -- variables..
    BEGIN
        -- statements..
    END;
$$ LANGUAGE plpgsql

With transactions

To make a procedure use a transaction, just add COMMIT at the of the block.

Calling

You can't call procedures within SELECT like you could in functions.

Deleting

triangle-exclamation

Usage

Returning a value

You can use INOUT parameter mode to "return" a value.

At the end of the procedure the value in this parameter will be returned.

Last updated