Passing a table to function - Mailing list pgsql-general

From sqlguru
Subject Passing a table to function
Date
Msg-id 5de5b661-d0e5-4e95-be7e-cc806bd847ba@t21g2000yqi.googlegroups.com
Whole thread Raw
Responses Re: Passing a table to function
Re: Passing a table to function
List pgsql-general
In SQL 2008, we could pass tables into stored procedures.
CREATE TABLE members -- Only username is required
(
     mem_username VARCHAR(25) NOT NULL PRIMARY KEY,
     mem_email VARCHAR(255),
     mem_fname VARCHAR(25),
     mem_lname VARCHAR(25)
);

CREATE TABLE TYPE member_table_type
(
       mem_username VARCHAR(25)
);

CREATE STORED PROCEDURE CreateMembers
         @members member_table_type READONLY
AS
     INSERT INTO [members]
     SELECT * FROM @members;

To execute this stored procedure, you would do:
DECLARE @members member_table_type;
INSERT INTO @members (mem_username)
VALUES( ('mem1'), ('mem2'), ('mem3') );
EXECUTE CreateMembers @members;


How would you accomplish this on Postgre 8.4? I know you can pass an
entire row to a function but that is not what I want. Notice that even
though the table has many columns (nullable), I'm only passing in the
username. With the ROW datatype in Postgre, you have to pass in all
the columns (null if no value).

This is what I have so far in Postgre:
CREATE FUNCTION create_members(IN var_members members)
BEGIN
     INSERT INTO members
     SELECTvar_members.mem_username, var_members.mem_email,
var_members.mem_fname, var_members.mem_lname;
END

SELECT create_members(ROW('mem1', NULL, NULL, NULL));


pgsql-general by date:

Previous
From: Sebastien FLAESCH
Date:
Subject: Normalize INTERVAL ouput format in a db driver
Next
From: Mohan Raj B
Date:
Subject: COALESCE not filtering well.