PL/PGSQL If Condition Statements

  • if
  • if then else
  • if then elsif then else

PL/pgSQL IF statement:

Syntax:

IF condition THEN
statement;
END IF;

Flowchart: IF statement

Example:

DO $$
DECLARE
a integer := 10;
b integer := 20;
BEGIN
IF a > b THEN
RAISE NOTICE 'a is greater than b';
END IF;
IF a < b THEN
RAISE NOTICE 'a is less than b';
END IF;
IF a = b THEN
RAISE NOTICE 'a is equal to b';
END IF;
END $$;

PL/pgSQL IF THEN ELSE statement:

Syntax:

IF condition THEN
statements;
ELSE
alternative-statements;
END IF;

Flowchart: IF ELSE statement

Example:

DO $$
DECLARE
a integer := 10;
b integer := 20;
BEGIN
IF a > b THEN
RAISE NOTICE 'a is greater than b';
ELSE
RAISE NOTICE 'a is not greater than b';
END IF;
END $$;

PL/pgSQL IF THEN ELSIF THEN ELSE statement:

Syntax:

IF condition-1 THEN
if-statement;
ELSIF condition-2 THEN
elsif-statement-2

ELSIF condition-n THEN
elsif-statement-n;
ELSE
else-statement;
END IF:

Flowchart: IF ELSIF ELSE statement

Example:

DO $$
DECLARE
a integer := 10;
b integer := 10;
BEGIN
IF a > b THEN
RAISE NOTICE 'a is greater than b';
ELSIF a < b THEN
RAISE NOTICE 'a is less than b';
ELSE
RAISE NOTICE 'a is equal to b';
END IF;
END $$;


Comments