Generating TRUNCATE orders

From: Laurent ROCHE <laurent_roche(at)yahoo(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Generating TRUNCATE orders
Date: 2007-10-03 16:59:04
Message-ID: 358774.16181.qm@web34413.mail.mud.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

I wanted to write a SELECT that generates a TRUNCATE TABLE for all the tables in a given schema.

So I wrote:
SELECT 'TRUNCATE TABLE '
UNION
SELECT 'my_schema.' || c.relname ||', '
FROM pg_namespace nc, pg_class c
WHERE c.relnamespace = nc.oid
AND c.relkind IN ('r' )
AND nc.nspname = 'my_schema'
ORDER BY relname

And this fails with the following message:
ERROR: column "relname" does not exist
SQL state:42703


If I run only the SELECT after the UNION that works as expected.
Of course, this is not a big deal as I copying and pasting this into a script file any way (and I will add the TRUNCATE TABLE manually).
But I don't understand why this does not work: the 2 SELECTs produce a single char column so from what I understand that should work ! ? !
If some body can explain I will be grateful.


PS: Of course, I realise the code produced by the SELECTs and UNION would not work straight away, because of the trailing comma !

Have fun,
L(at)u
The Computing Froggy

_____________________________________________________________________________
Ne gardez plus qu'une seule adresse mail ! Copiez vos mails vers Yahoo! Mail

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Richard Huxton 2007-10-03 17:11:15 Re: Generating TRUNCATE orders
Previous Message Alvaro Herrera 2007-10-03 16:04:59 Re: Easier string concat in PL funcs?