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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:03:28+00:00 2026-05-31T01:03:28+00:00

How do I use an array to insert to a table in a function?

  • 0

How do I use an array to insert to a table in a function?

CREATE TABLE places
(
id      SERIAL PRIMARY KEY
,location   VARCHAR(100) NOT NULL
,timestamp  TIMESTAMP
,person_id  INTEGER NOT NULL REFERENCES people ON UPDATE...
);

CREATE TABLE people
(
id      SERIAL PRIMARY KEY
,name   VARCHAR(100) NOT NULL
);

I am trying to do a function where I would do:

SELECT function(location_name, now(), '{1,2,3}');

This would INSERT 3 records into the places table for each person_id in the array, with the same location and timestamp. The main problem is that the length of the array can be dynamic, so the function should be able to handle:

SELECT function(location_name, now(), '{3,5,7,8,10}');

I don’t know where to start in making the INSERT dynamic and extracting the ids from the array. Unest can be used to INSERT INTO variables in DECLARE but the dynamic aspect cannot be done. I hope I made this clear.

  • 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-31T01:03:30+00:00Added an answer on May 31, 2026 at 1:03 am

    Writing a plpgsql function is always a possibility (and I do it a lot). But this case can be solved simpler:

    INSERT INTO places (location, ts, person_id) 
    SELECT 'some_location', now(), unnest('{1,2,3}'::int4[])
    

    I replaced your column name timestamp with ts because I don’t think it is a good idea to use a type name as identifier. It leads to confusing side effects and error messages.


    If you need to JOIN to the result set of an unnest() function – like your question seems to imply (but the problem turned out to be simpler) – you have to make it a sub-query:

    INSERT INTO places (location, ts, name)
    SELECT 'some_location', now(), p.name
    FROM  (SELECT unnest('{1,2,3}'::int4[]) AS id) AS x
    JOIN   people p USING (id);
    

    I added the column name, which is not in your model, just do demonstrate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a function that takes a table name and a key value array
I'd like to use a foreach loop to insert a _POST associative array into
Is it okay to use array without single or double quotion like $array[key]? I
If I use an array of linked list to implement a hash table, the
I am trying to use the unnest function in insert . While doing so,
I am trying to use mysql NOW() function when I insert a record with
I'm trying to insert over 100,000 records into an Oracle 9i table with no
Or better said: When to use array as a field data type in a
I'm making a top-bottom queue. Do I use array or arraylist?
I use an array to store fieldvalues. To easily add and access elements I

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.