Re: CREATE SCHEMA $1?

From: "Chris Travers" <chris(at)travelamericas(dot)com>
To: "wtf" <wtf(at)despammed(dot)com>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: CREATE SCHEMA $1?
Date: 2003-11-29 02:56:09
Message-ID: 01d901c3b63f$a6700560$6444053d@SAMUEL
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


wtf wrote:
> I to create schema with arbitrary name but it seems CREATE SCHEMA doesn't
> accept anythig different from a constant as an argument. I've tried with a
> function (CREATE SCHEMA some_func();), a SELECT (CREATE SCHEMA (SELECT
> somename FROM sometable);) or as a function (CREATE SCHEMA $1; inside a
> function definition) but the parser always throw an error at the first
> carachter after "SCHEMA".
>
This problem exists because the utility statements currently do not accept
arguements. This means you have two choices:
1) Use perl (I think this will work)
2) Use plpgsql, generate a dynamic query and use the execute statement to
do this. Something like:
CREATE FUNCTION my_c_schema(NAME)
RETURNS BOOL AS '
DECLARE
schema_name ALIAS OF $1;
BEGIN
EXECUTE ''CREATE SCHEMA " || schema_name;
END;
' LANGUAGE PLPGSQL;

Note that the execute string is enclosed in double single-quotes. Then you
can call this function as:
select my_c_schema(schema_name);
and it will create a schema named schema_name.

Best Wishes,
Chris Travers

P.S. It would be REALLY nice if the documentation on SQL language functions
contained a note about this problem :)

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Richard Welty 2003-11-29 03:19:51 Re: [ADMIN] Misplaced modifier in Postgresql license
Previous Message Chris Travers 2003-11-29 02:41:17 Re: PostgreSQL Advocacy, Thoughts and Comments