hhkk
hhkk

Reputation: 51

Postgresql in Pgadmin iii (Function IF/ELSE)

I am trying to create a SELECT FROM inside an IF statement inside a function. Here is what i have so far

CREATE OR REPLACE FUNCTION fn_check_marriage(partner_id INTEGER)    
    RETURNS VARCHAR(10) AS
$$
DECLARE
    result text;

BEGIN
    result := ''

    IF EXISTS (SELECT partner_id_1 FROM marriages WHERE divorce_date IS NULL) THEN

    result := 'True';

    ELSE

    result := 'False';

    END IF;

RETURN result;

END;
$$
LANGUAGE 'plpgsql'

But it keeps on giving me errors, most recently "error at or near IF". Any thoughts on how to make this work if at all possible? Thanks in advance

Upvotes: 0

Views: 2392

Answers (1)

OBi
OBi

Reputation: 116

Do a perform then check found or not:

PERFORM partner_id_1 FROM marriages WHERE divorce_date IS NULL;
IF FOUND THEN
  result := 'True';
ELSE
  result := 'False';
END IF;

Upvotes: 1

Related Questions