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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:27:31+00:00 2026-06-08T09:27:31+00:00

I have a Postgresql PL/pgSQL function that has a double nested FOR loop, where

  • 0

I have a Postgresql PL/pgSQL function that has a double nested FOR loop, where I want to set column names dynamically. However I can’t find a way of accessing RECORD columns programmatically.

I’ll skip straight to an example with some code:

FOR loop_helper1 IN SELECT
  id, name1, name2, name3, nameN,
  FROM table1
  LOOP
    FOR loop_helper2 IN SELECT name FROM table2 LOOP
      -- I want to set values of columns in loop_helper1,
      -- with the column name given by loop_helper2.name
      -- An EXECUTE would not allow me to do this:
      EXECUTE 'loop_helper1.' || loop_helper2.name || ':= function_call(123);'
      -- (Eg. 'loop_helper1.name2 := function_call(123);')
      -- However, this produces: ERROR:  syntax error at or near "loop_helper1"
    END LOOP;
END LOOP;

Any ideas?

Surely there’s gotta be a way to do this, but I just can’t seem to find it. All help and suggestions are appreciated. Thanks.

  • 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-08T09:27:33+00:00Added an answer on June 8, 2026 at 9:27 am

    I think you probably want the function we devised under this related question.
    With this function, your code fragment might look like this:

    
    FOR loop_helper1 IN
        SELECT id, name1, name2, name3, nameN,
        FROM   table1
    LOOP
        FOR loop_helper2 IN
            SELECT name
            FROM   table2
        LOOP
           loop_helper1 := public.setfield (loop_helper1
                                          ,loop_helper2.name
                                          ,function_call(123));
        END LOOP;
    END LOOP;
    

    There may be simpler ways, especially if you want to assign a whole composite type …

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

Sidebar

Related Questions

I have postgresql (in perlu) function getTravelTime(integer, timestamp), which tries to select data for
I have a PostgreSQL, php5, running on Apache2.0 (on a Windows machine.) I want
Background: I have a PostgreSQL (v8.3) database that is heavily optimized for OLTP. I
That title is presumably awfully worded. I have some PostgreSQL tables. There is a
I have a parameterized postgresql query that users interact with through a PHP front-end.
I have 3 tables Person, Names, and Notes. Each person has multiple name and
I have a function (stored procedure) defined in a database that I would like
We have some PL/pgSQL stored procedures in our DB (PostgreSQL 9.x). These are strictly
PostgreSQL 8.4 on Linux. I have a function: CREATE OR REPLACE FUNCTION production.add_customer (
I have a number of tables that use the Postgres Partitioning feature. I want

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.