1

I don't want a function or anything. I just want to run/test a simple script with some PL/pgSQL features in it. Something like the following:

IF 1=1 THEN
    SELECT concat('Hello', 'World');
END IF;

I'm sure the syntax is correct, but it doesn't work in the normal "Query Editor" like I'd do it in Microsoft SQL Server. I get the following error:

[Err] ERROR:  syntax error at or near "IF"
LINE 1: IF 1=1 THEN
        ^

I'm running Navicat for PostgreSQL version 11.1.13 (64-bit) on a Windows 10 machine. select version() says the following:

PostgreSQL 9.4.4 on x86_64-unknown-linux-gnu, compiled by gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2, 64-bit

edit:

I just realized that this question might be the victim of the X-Y Problem. What I really want to do is to test the following way of inserting values ignoring duplicates:

BEGIN
    INSERT INTO db_table (tbl_column) VALUES (v_tbl_column);
EXCEPTION WHEN unique_violation THEN
    -- Ignore duplicate inserts.
END;

I found it here: Optimal way to ignore duplicate inserts?

I get the following error:

[Err] ERROR:  syntax error at or near "INSERT"
LINE 2:         INSERT INTO db_table (tbl_column) VALUES ('test');
                ^
2
  • 1
    INSERT INTO db_table (tbl_column) select 'test' where not exists (select 1 from db_table where tbl_column = 'test'). Or wait for 9.5 and use insert on conflict. See also here: stackoverflow.com/q/1109061/330315
    – user1822
    Nov 28, 2015 at 22:14
  • Thanks. I did find the 9.5 feature, looking forward to it. Nov 28, 2015 at 22:31

1 Answer 1

1
DO $$
BEGIN
    INSERT INTO t1 (id) VALUES (6);
EXCEPTION WHEN unique_violation THEN
    -- Ignore duplicate inserts.


END$$;

You can not use IF outside procedure code, but Your construction possible run like this

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.