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');
                ^

1 Answers1

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

a_vlad
  • 3,705
  • 2
  • 13
  • 17