plpgsql return select from multiple tables

From: "Artis Caune" <artis(dot)caune(at)gmail(dot)com>
To: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: plpgsql return select from multiple tables
Date: 2008-09-10 14:20:07
Message-ID: 9e20d71e0809100720p6aa7d6d1p8e95043fad6c8713@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

What is the correct way of writing plpgsql function which needs return
columns from multiple tables?

e.x.:
SELECT email FROM emails WHERE id = 1
SELECT backend FROM backends WHERE id = 1

I need plpgsql function return both email and backend in one line, like:
SELECT email, backend FROM ...

I do like this:

CREATE OR REPLACE FUNCTION get_user_data( INT )
RETURNS SETOF RECORD AS $$
DECLARE
v_email RECORD;
v_backend RECORD;
BEGIN
SELECT email
INTO v_email
FROM emails
WHERE id = $1;

SELECT backend
INTO v_backend
FROM backends
WHERE id = $1;

RETURN QUERY SELECT v_email AS email,
v_backend AS backend;
END;
$$ LANGUAGE 'plpgsql' SECURITY DEFINER;

and then doing selects:
SELECT * FROM get_user_data('${id}') AS (email VARCHAR, backend VARCHAR)

Is it okay, there will be a lot of those queries?

--
regards,
Artis Caune

<----. CCNA
<----|====================
<----' didii FreeBSD

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Alvaro Herrera 2008-09-10 14:20:24 Re: Autocommit, isolation level, and vacuum behavior
Previous Message Markova, Nina 2008-09-10 14:14:50 Re: [GENERAL] FW: How to upload data to postgres