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 7053223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:28:44+00:00 2026-05-28T03:28:44+00:00

We often use quick one-off SQL files to insert or update data in an

  • 0

We often use quick one-off SQL files to insert or update data in an existing database. The SQL is usually written by a developer, tested on the development system, and then imported in the production DB with psql -U dbuser dbname < file.sql.

A (trivial) example might look like this:

INSERT INTO employees (
    company_id,
    name,
    position,
    created_by,
    last_modified_by
) VALUES
(
    (SELECT id FROM companies WHERE name = 'Acme Fellowship'),
    'Frodo Baggins',
    'Ring bearer',
    (SELECT id FROM users WHERE login = 'admin'),
    (SELECT id FROM users WHERE login = 'admin')
),
(
    (SELECT id FROM companies WHERE name = 'Acme Fellowship'),
    'Samwise Gamgee',
    'Rope bearer',
    (SELECT id FROM users WHERE login = 'admin'),
    (SELECT id FROM users WHERE login = 'admin')
),
(
    (SELECT id FROM companies WHERE name = 'Acme Fellowship'),
    'Peregrin Took',
    'Ent rider',
    (SELECT id FROM users WHERE login = 'admin'),
    (SELECT id FROM users WHERE login = 'admin')
);

While this works, there’s a lot of repetitive code in the subqueries. It would be nice (more efficient and less error prone) to store the relevant values for companies.id and users.id in temporary variables. In this construed example, the performance difference is likely minimal, but in practice we do have more complex queries and updates, and there are often more than three updated/inserted records.

The same example written for MySQL looks like this:

SELECT @company_id := id FROM companies WHERE name = 'Acme Fellowship';
SELECT @admin_id := id FROM users WHERE login = 'admin';
INSERT INTO employees (
    company_id,
    name,
    position,
    created_by,
    last_modified_by
) VALUES
(@company_id, 'Frodo Baggins',  'Ring bearer', @admin_id, @admin_id),
(@company_id, 'Samwise Gamgee', 'Rope bearer', @admin_id, @admin_id),
(@company_id, 'Peregrin Took',  'Ent rider',   @admin_id, @admin_id);

Is there any way to achieve something similar in PostgreSQL?

What I’ve looked at:

  • psql’s session variables (with \set): cannot be used to store query results
  • plpgsql: can only be used in a procedure (we’re still running 8.4)
  • temporary tables: I can’t see how to use them without creating ugly and convoluted statements

If there is no direct equivalent for Postgres, what do you think would be the least clumsy way to produce update files of this kind?

  • 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-05-28T03:28:45+00:00Added an answer on May 28, 2026 at 3:28 am

    Consider using CTEs or subqueries to query values once and inserted them many times.
    This way, you can replace MySQL style variables with standard SQL.

    INSERT INTO employees
          (company_id, name, position, created_by, last_modified_by)
    SELECT c.id      , name, position, u.id      , u.id
    FROM  (SELECT id FROM companies WHERE name = 'Acme Fellowship') c
         ,(SELECT id FROM users WHERE login = 'admin') u
         ,(VALUES
             ('Frodo Baggins',  'Ring bearer') 
            ,('Samwise Gamgee', 'Rope bearer')
            ,('Peregrin Took',  'Ent rider')
          ) v(name, position)
    

    Assuming that companies.name and users.login are, in fact, unique. Multiple hits would multiply the rows to be inserted.
    Read about the INSERT command in the manual.


    Here is my test setup with temporary tables in case anyone wants to have a quick look:

    CREATE TEMP TABLE companies (id int, name text);
    INSERT INTO companies VALUES (17, 'Acme Fellowship');
    
    CREATE TEMP TABLE users (id int, login text);
    INSERT INTO users VALUES (9, 'admin');
    
    CREATE TEMP TABLE employees (
     company_id int
    ,name text
    ,position text
    ,created_by int
    ,last_modified_by int);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I often use Excel with pivot tables based on .cub files for OLAP-type analysis.
I often use the python interpreter for doing quick numerical calculations and would like
I often use notepad++ for editing of the csproj files. And I always need
I often use the excellent find program in Bash to list files with certain
I often use gVim and Vim to view data tables in which the top
Just a quick database design question: Do you ALWAYS use an ID field in
Quick question regarding database design; Given I'm storing User data in a database, I
I don't use SQL Server Cursors often but when I do, I always have
I use the favorites in Windows Explorer quite often to gain quick access to
I often use ajax to update or delete objects in the controller. Now, there

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.