cildoz
cildoz

Reputation: 436

If then clause postgresql

I've defined this function:

SELECT dblink_connect('conne1', 'dbname=bdp3e1');
SELECT dblink_connect('conne2', 'dbname=bdp3e2');

CREATE OR REPLACE FUNCTION insertEditorial(nombre VARCHAR(100), CIF INTEGER, ubicacion VARCHAR(50))
RETURNS void AS
$insertEditorial$
BEGIN
    IF ubicacion IS NULL THEN
        dblink_exec('conne2', 'INSERT INTO Editorial VALUES (nombre, CIF);');
    ELSE
        dblink_exec('conne1', 'INSERT INTO Editorial VALUES (nombre, CIF, ubicacion);');
    ENDIF;
END;
$insertEditorial$ LANGUAGE plpgsql;

But when executed, postgreSQL returns the following error:

psql:PR3_Procedures.sql:14: ERROR:  syntax error at or near "dblink_exec"
LINE 6:         dblink_exec('conne2', 'INSERT INTO Editorial VALUES ...

What's wrong with it?

Upvotes: 0

Views: 92

Answers (1)

sticky bit
sticky bit

Reputation: 37472

Try to use PERFORM and change ENDIF to END IF.

...
IF ubicacion IS NULL THEN
    PERFORM dblink_exec('conne2', 'INSERT INTO Editorial VALUES (nombre, CIF);');
ELSE
    PERFORM dblink_exec('conne1', 'INSERT INTO Editorial VALUES (nombre, CIF, ubicacion);');
END IF;
...

Upvotes: 1

Related Questions