Thursday, 22 August 2013

How to define tables and columns as arguments in a pgsql function?

How to define tables and columns as arguments in a pgsql function?

It must be simple, but I'm making my first steps into Postgres functions
and I can't find anything that works...
I'd like to create a function that will modify a table and/or a column and
I can't find the right way of specifying my tables and columns as
arguments in my function.
Something like:
CREATE OR REPLACE FUNCTION foo(t table)
RETURNS void AS $$
BEGIN
alter table t add column test varchar(20);
END;
$$ LANGUAGE PLPGSQL;
select foo(some_table)
In another case, I'd like to have a function that alter a certain column
from a certain table:
CREATE OR REPLACE FUNCTION foo(t table, c column)
RETURNS void AS $$
BEGIN
UPDATE t SET c = "This is a test";
END;
$$ LANGUAGE PLPGSQL;
Is it possible to do that?
Thanks!!

No comments:

Post a Comment