Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL IF statement

How can I do such query in Postgres?

IF (select count(*) from orders) > 0 THEN   DELETE from orders ELSE    INSERT INTO orders values (1,2,3); 
like image 297
Vladimir Tsukanov Avatar asked Jul 02 '12 18:07

Vladimir Tsukanov


People also ask

What is the correct syntax of writing if/then statement in PL pgSQL?

The IF statement is part of the default procedural language PL/pgSQL. You need to create a function or execute an ad-hoc statement with the DO command. You need a semicolon ( ; ) at the end of each statement in plpgsql (except for the final END ). You need END IF; at the end of the IF statement.

How do you write a case statement in PostgreSQL?

Syntax: CASE WHEN boolean-expression-1 THEN statements [ WHEN boolean-expression-2 THEN statements ... ] [ ELSE statements ] END CASE; The searched CASE statement executes statements based on the result of Boolean expressions in each WHEN clause.

Can we use CASE statement in where clause in PostgreSQL?

The PostgreSQL CASE expression is the same as IF/ELSE statement in other programming languages. It allows you to add if-else logic to the query to form a powerful query. Since CASE is an expression, you can use it in any places where an expression can be used e.g., SELECT , WHERE , GROUP BY , and HAVING clause.

How many types of conditional statements are present in PG Plsql?

PL/pgSQL provides you with three forms of the if statements.


2 Answers

DO $do$ BEGIN    IF EXISTS (SELECT FROM orders) THEN       DELETE FROM orders;    ELSE       INSERT INTO orders VALUES (1,2,3);    END IF; END $do$ 

There are no procedural elements in standard SQL. The IF statement is part of the default procedural language PL/pgSQL. You need to create a function or execute an ad-hoc statement with the DO command.

You need a semicolon (;) at the end of each statement in plpgsql (except for the final END).

You need END IF; at the end of the IF statement.

A sub-select must be surrounded by parentheses:

    IF (SELECT count(*) FROM orders) > 0 ... 

Or:

    IF (SELECT count(*) > 0 FROM orders) ... 

This is equivalent and much faster, though:

    IF EXISTS (SELECT FROM orders) ... 

Alternative

The additional SELECT is not needed. This does the same, faster:

DO $do$ BEGIN    DELETE FROM orders;    IF NOT FOUND THEN       INSERT INTO orders VALUES (1,2,3);    END IF; END $do$ 

Though unlikely, concurrent transactions writing to the same table may interfere. To be absolutely sure, write-lock the table in the same transaction before proceeding as demonstrated.

like image 172
Erwin Brandstetter Avatar answered Sep 20 '22 10:09

Erwin Brandstetter


Just to help if anyone stumble on this question like me, if you want to use if in PostgreSQL, you use "CASE"

select      case         when stage = 1 then 'running'         when stage = 2 then 'done'         when stage = 3 then 'stopped'     else          'not running'     end as run_status from processes 
like image 45
Mahmood Avatar answered Sep 18 '22 10:09

Mahmood