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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:14:48+00:00 2026-06-03T04:14:48+00:00

I’m trying to insert data into a table using a plpgsql function or stored

  • 0

I’m trying to insert data into a table using a plpgsql function or stored procedure. However, I want to insert multiple records at once. I now these will all be VARCHAR so I thought I could use a function formed like function(tablename VARCHAR, records VARCHAR[][]). But then I found out multidimensional array support in plpgsql isn.t quite so fantastic.

This is what my function looks like at the moment. This doesn’t produce the result I’m looking for.
When I ask

SELECT insert_data('tennis', ARRAY[ARRAY['1','2'], ARRAY['3','4']])

I get the following error

ERROR:  syntax error at or near "{"
LINE 1: INSERT INTO tennis VALUES (null, {{1}}), (null, {{3}});
                                         ^
QUERY:  INSERT INTO tennis VALUES (null, {{1}}), (null, {{3}});
CONTEXT:  PL/pgSQL function "insert_data" line 26 at EXECUTE statement

However I am expecting a query like

INSERT INTO tennis VALUES (null, '1', '2'), (null, '3', '4');

which would work because table tennis has this structure.

CREATE OR REPLACE FUNCTION insert_data (dsetname_in VARCHAR, records VARCHAR[][])
RETURNS BOOLEAN AS $PROC$
DECLARE
    insertquery TEXT;
    val VARCHAR;
    i INT;
    j INT;
BEGIN
    insertquery = $$INSERT INTO $$ || dsetname_in || $$ VALUES $$;
    FOR i IN array_lower(records, 1)..array_upper(records, 1)
    LOOP
        insertquery = insertquery || $$(null, $$;
        FOR j IN array_lower(records[i:i], 1)..array_upper(records[i:i], 1)
        LOOP
            val = records[i:i][j:j];
            insertquery = insertquery || val;
            IF j <> array_upper(records[i:i], 1) THEN
                insertquery = insertquery || $$, $$;
            END IF;
        END LOOP;
        insertquery = insertquery || $$)$$;
        IF i <> array_upper(records, 1) THEN
            insertquery = insertquery || $$, $$;
        END IF;
    END LOOP;
    insertquery = insertquery || $$;$$;
    EXECUTE insertquery;
    RETURN TRUE;
END;$PROC$ LANGUAGE 'plpgsql';
  • 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-03T04:14:49+00:00Added an answer on June 3, 2026 at 4:14 am

    I’m dubious about the value of this whole approach because I don’t see that it adds any useful level of abstraction; but if you must do it, and all your values are character strings, I think the cleanest approach is this:

    CREATE OR REPLACE FUNCTION insert_data(dsetname_in text, records text[])
    RETURNS VOID LANGUAGE plpgsql AS $PROC$
    DECLARE
      insertquery text;
      i int;
    BEGIN
      insertquery := 'INSERT INTO ' || dsetname_in || ' VALUES ';
      FOR i IN array_lower(records, 1)..array_upper(records, 1)
      LOOP
        insertquery := insertquery || '(null, ' || records[i] || '),';
      END LOOP;
      insertquery := left(insertquery, char_length(insertquery) - 1);
      EXECUTE insertquery;
    END;
    $PROC$;
    

    You can then call it like this, which seems cleaner than what you showed for the nested arrays:

    SELECT insert_data('tennis',
                       ARRAY[$$'1','2'$$,
                             $$'3','4'$$]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,

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.