Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8565039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:19:57+00:00 2026-06-11T17:19:57+00:00

When I have two INSERT SQL statements (see below) within a begin; and commit;

  • 0

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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:19:58+00:00Added an answer on June 11, 2026 at 5:19 pm

    One way would be to use a data-modifying CTE and pack the two INSERTs into one command. Requires PostgreSQL 9.1 or later:

    WITH x AS (
       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 *;
    

    However, you do get values back with RETURNING * in any case. Just read them in before sending COMMIT. Sent in as batch, only results from the last command are returned – which would be the result of COMMIT, if you send all commands as one batch.

    Hold back COMMIT; until you have received the results from the second INSERT.


    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 INSERT use RETURNING *expressions* INTO [STRICT] *target*.

    Consider this simple demo:

    CREATE TABLE foo (foo_id serial, bar text);
    
    CREATE OR REPLACE FUNCTION f_foo()
      RETURNS void LANGUAGE plpgsql AS
    $BODY$
    DECLARE
       foo_var foo; -- type name = table name, fits return type of RETURNING *
       -- or use a generic type record
    BEGIN
    
       INSERT INTO foo (bar)
       VALUES ('baz')
       RETURNING *
       INTO foo_var;
    
       RAISE NOTICE 'New id is: %', foo_var.foo_id;
    
       -- do stuff with foo_var
    
       END;
       $BODY$;
    
    SELECT f_foo();
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP page that runs two SQL insert statements at the beginning
I have a sql file which has a lot of insert statements (over 3000+).
I have two insert statements, almost exactly the same, which run in two different
Let's say I have two SQL queries: INSERT INTO tableA VALUES ('', 'mike', '21')
I have two queries. The first one is.. INSERT INTO balik ( balik_date, balik_time,
I'm using devexpress and i have two problems. My insert stored procedure work fine.
Good Morning. I have two tables, and one references the other. When I insert
I have an INSERT query that is pulling data from two tables and inserting
How would I combine the following two statements to create a valid SQL query?
What would be the insert SQL statement to merge data from two tables. For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.