PL/pgSQL

Declaring Variables

DO
$$
    DECLARE
        mynum      integer     := 89;
        first_name varchar(20) := 'Uday';
        hire_date  date        := '2020-01-01';
        start_time timestamp   := NOW();
        emptyvar   integer;

    BEGIN
        RAISE NOTICE 'My variable % % % % %',
        mynum, first_name, hire_date,
        start_time, emptyvar ;
    END;
$$

Parameters to Function


CREATE OR REPLACE FUNCTION function_name 
    (INT, INT) RETURNS INT as
$$
    DECLARE
        x alias for $1;
        y alias for $2;

    BEGIN
        --
    END;
$$

Assigning value from result into variable

Function Parameter w/t IN and OUT

Nested functions

Returning ResultSet from function

Conditional Statement inside functions

Default Parameters

Switch Case Example

Loops in PLPGSQL

Loops in range exaple

Reverse Loops

Iterating over result set

Loop with Exit condition

Declaring arrays in PLPGSQL

While Loop in PLPGSQL

Returning specific Query from column

If data not found condition

Throwing execption on condition

Throwing execption example

Last updated

Was this helpful?