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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:05:06+00:00 2026-05-19T02:05:06+00:00

I want to write a stored procedure that returns a ‘flattened’ object. By ‘flattening’,

  • 0

I want to write a stored procedure that returns a ‘flattened’ object. By ‘flattening’, I am essentially selecting a set of rows, and returning specific fields in the rows, into the data returned from the function.

The code below explains what I am trying to do

CREATE TABLE user (id int, school_id int, name varchar(32));

CREATE TYPE my_type (user1_id int, user1_name varchar(32), user2_id int, user2_name varchar(32));

CREATE OR REPLACE FUNCTION get_two_users_from_school(schoolid int)
RETURNS my_type AS $$
DECLARE
 result my_type
 temp_result user
BEGIN
  -- for purpose of this question assume 2 rows returned
  SELECT id, name INTO temp_result FROM user where school_id = schoolid LIMIT 2;
  -- Will the (pseudo)code below work?:
  result.user1_id := temp_result[0].id ;
  result.user1_name := temp_result[0].name ;
  result.user2_id := temp_result[1].id ;
  result.user2_name := temp_result[1].name ;
  return result ;
END
$$ language plpgsql

I have two questions:

  • Am I using the correct data type for variable temp_result
  • Am I accessing the rows correctly (using array indexing)?
  • 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-19T02:05:07+00:00Added an answer on May 19, 2026 at 2:05 am

    Am I accessing the rows correctly (using array indexing)?

    No, you need to loop through the result using a cursor which is nicely explained in the manual:
    http://www.postgresql.org/docs/current/static/plpgsql-control-structures.html#PLPGSQL-RECORDS-ITERATING

    Something like this should work:

    DECLARE 
      temp_result RECORD;
      row_counter integer;
    BEGIN
      row_counter := 1;
      FOR temp_result IN SELECT id, name FROM user where school_id = schoolid LIMIT 2 LOOP
        IF row_counter = 1 THEN
           result.user1_id := temp_result.id;
           result.user1_name = temp_result.name;
        END IF;
        IF row_counter = 2 THEN
           result.user2_id := temp_result.id;
           result.user2_name = temp_result.name;
        END IF;
        row_counter := row_counter + 1;
      END LOOP;
    
      return result;
    END;
    

    Btw: having a table named “user” is not a really good idea as user is a reserved word and might cause some problems in the long run.

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

Sidebar

Related Questions

I want to write a stored procedure that will accept a parameter of @searchString.
I want to write a stored procedure that works something like this: SELECT *
I want to write a PLSQL stored procedure that accepts a table name as
I want to write a stored procedure that lists the name of all the
I want to write a stored procedure that accept an XML parameter, parsing it's
I want to write a stored procedure for my Tables that have a similar
I want to write a stored procedure. In that stored procedure, I want to
I have a stored procedure that adds an object to the database and returns
I want to write a stored procedure that queries XML files after I have
I want to write a stored procedure to increment the value of an int

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.