When I have two INSERT SQL statements (see below) within a begin; and commit; transaction then the RETURNING * does not return anything but if I take out the begin; and commit; the RETURNING * does return the inserted record.
How can I get the RETURNING * to work within a transaction?
BEGIN;
INSERT INTO gis_field_configuration
(level_unique_name, level_name_caption, use_for_charts, use_as_displayby,
displayby_label, data_type, level_help_text)
VALUES (
'[john].[john]',
'john',
'false',
'false',
'',
'text',
'help text'
);
INSERT INTO gis_field_configuration_bycube
(cube, level_unique_name)
VALUES (
'Instruments',
'[john].[john]'
) RETURNING *;
COMMIT;
One way would be to use a data-modifying CTE and pack the two
INSERTs into one command. Requires PostgreSQL 9.1 or later:However, you do get values back with
RETURNING *in any case. Just read them in before sendingCOMMIT. Sent in as batch, only results from thelastcommand are returned – which would be the result ofCOMMIT, if you send all commands as one batch.Hold back
COMMIT;until you have received the results from the secondINSERT.In a plpgsql function
A function runs inside a transaction automatically . You don’t need explicit BEGIN / COMMIT. To reuse values you get back from an
INSERTuseRETURNING *expressions* INTO [STRICT] *target*.Consider this simple demo: