Search Postgresql Archives

Passing a table to function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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));


-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux