Re: DO like block for for anonymous procedures

From: Vijaykumar Jain <vijaykumarjain(dot)github(at)gmail(dot)com>
To: pgsql-general <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: DO like block for for anonymous procedures
Date: 2021-07-28 17:14:48
Message-ID: CAM+6J95tHgCRpLvRe4EMG7UEhinTgZ7i8etda=jn_Lx4fsYpUQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

please ignore, i overlooked the obvious.

truncate table t;
TRUNCATE TABLE
postgres=# do $$
declare valuelist int[] := ARRAY[1,2,3,4,5,1]; -- purposely inserting
duplicate that would rollback everything
declare i int;
begin
for i in select k from unnest(valuelist) p(k) loop
insert into t values(i);
raise notice 'trying to insert %', i; commit;
end loop;
end; $$;
NOTICE: trying to insert 1
NOTICE: trying to insert 2
NOTICE: trying to insert 3
NOTICE: trying to insert 4
NOTICE: trying to insert 5
ERROR: duplicate key value violates unique constraint "t_pkey"
DETAIL: Key (id)=(1) already exists.
CONTEXT: SQL statement "insert into t values(i)"
PL/pgSQL function inline_code_block line 6 at SQL statement
postgres=# table t;
id
----
1
2
3
4
5
(5 rows)

sorry.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Robert Haas 2021-07-28 17:28:08 Re: Have I found an interval arithmetic bug?
Previous Message Vijaykumar Jain 2021-07-28 17:06:56 DO like block for for anonymous procedures