scripts in Postgres

From: "Craig Bryden" <postgresql(at)bryden(dot)co(dot)za>
To: "pgsql" <pgsql-general(at)postgresql(dot)org>
Subject: scripts in Postgres
Date: 2005-05-02 11:21:48
Message-ID: 000701c54f09$2235d360$0200a8c0@amd2800
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi

I am fairly new to Postgres and am struggling to understand one concept. If
I wish to use pl/pgsql, must it be in a function?
An example of where I would not want it to be in a function is:

I have a CREATE TABLE statement that I want to execute. But the script must
first check for the existence of the table. I wrote the following code, but
it errors on the first word (IF). Please tell me how to do this:

IF EXISTS (SELECT * FROM information_schema.tables WHERE table_Name =
'tb_Messages')
DROP TABLE tb_Messages;

CREATE TABLE tb_Messages (
MessageID bigserial PRIMARY KEY,
From varchar(255),
To varchar(255),
DateSent timestamp not null DEFAULT current_timestamp,
Subject varchar(255) NULL,
MessageBody Text null,
IsRead smallint default 0,
DeleteFlag smallint default 0,
AdditionalInfo int NULL,
ReplyToMessage bigint NULL

) WITHOUT OIDS;

Thanks
Craig

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Marco Colombo 2005-05-02 11:31:25 Re: Python DB-API 2.0 oddity (was: I receieved an example of
Previous Message Karsten Hilbert 2005-05-02 09:56:05 Python DB-API 2.0 oddity (was: I receieved an example of Rekall script)