Is it possible to execute PL/pgSQL not function wrapped?

From: Thiemo Kellner <thiemo(at)thiam(dot)ch>
To: pgNovice <pgsql-novice(at)postgresql(dot)org>
Subject: Is it possible to execute PL/pgSQL not function wrapped?
Date: 2003-12-25 09:32:34
Message-ID: 200312251032.44530.thiemo@thiam.ch
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

for development of a installation script I want a PL/pgSQL script that cleans
up a database (drop of all objects there in). I tried to execute PL/pgSQL
code directly within the script:
DECLARE
views RECORD;
tables RECORD;
str_statement TEXT;
BEGIN
-- drop all views
FOR views IN
SELECT
viewname,
schemaname
FROM
pg_views
WHERE
viewowner = 'lyrix_dba'
LOOP
str_statement := ''drop view '' || views.schemaname || ''.'';
str_statement := str_statement || views.viewname;
EXECUTE str_statement;
commit;
END LOOP;


-- drop all tables
FOR tables IN
SELECT
tablename,
schemaname
FROM
pg_tables
WHERE
tableowner = 'lyrix_dba'
LOOP
str_statement := ''drop table '' || tables.schemaname || ''.'';
str_statement := str_statement || tables.tablename;
EXECUTE str_statement;
commit;
END LOOP;

RETURN 0;
END;

However, this results in a number of parse errors. I suppose I needed to tell
PostgrSQL what language the stuff is written in, but how? So is it possible
to do it the way intend or do I need to wrap the code in a function that I
create in the script, and drop the function after usage anyway?

Cheers

Thiemo

- --
root ist die Wurzel allen Übels
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQE/6q65ood55Uv4ElYRAhl7AJ0W0wkbXhpC9YbPj0dq1PhPyihU3gCfd5am
rMm/kvuoEDVrQagO1pa27FQ=
=yfyW
-----END PGP SIGNATURE-----

Browse pgsql-novice by date

  From Date Subject
Next Message Paul Makepeace 2003-12-26 03:52:42 strptime string for timestamp with time zone
Previous Message Michael Glaesemann 2003-12-24 13:52:01 Thanks! Re: How to run 2+ versions of PostgreSQL concurrently