PL/PGSQL If Condition Statements



PL/pgSQL IF Condition Statements:
  • if
  • if then else 
  • if then elsif then else

Syntax:


IF condition THEN

statement;

END IF;



The following flowchart illustrates the simple IF statement.
 


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:

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

The following flowchart illustrates the IF ELSE statement.

 

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:

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:

The following flowchart illustrates the IF ELSIF ELSE statement.

 

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

Popular posts from this blog

PostgreSQL pg_pool-II Installation and Configuration

PostgreSQL Migration Using MTK

PostgreSQL Pages and Tuples