B
B
beduin012018-01-29 13:48:46
PostgreSQL
beduin01, 2018-01-29 13:48:46

What's wrong with the if request?

Learning if queries in PostgreSQL.
I can not understand why it swears at an error in the request:

IF SELECT age FROM peoples where id = 2 > 1 THEN
SELECT 1+1
END IF;

Writes: "approximate position of the If error"

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
OKyJIucT, 2018-01-29
@OKyJIucT

Maybe so?

DO
$do$
BEGIN
IF EXISTS(SELECT age FROM peoples where id = 2 > 1) THEN
SELECT 1+1
END IF;
END
$do$

Taken from here https://stackoverflow.com/questions/208401/postgresql-if-statement

K
krypt3r, 2018-01-30
@krypt3r

DECLARE v_age INT;
...
SELECT INTO v_age age FROM peoples where id = 2;
IF v_age > 1 THEN
    SELECT 1 + 1;
END IF;

Or
PERFORM age FROM peoples where id = 2;
IF FOUND THEN
    SELECT 1 + 1;
END IF;

Well, with EXISTS you can come up with

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question