pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Sydney-Smith <richard@ibisaustralia.com>
To: pgsql-sql@postgresql.org
Subject: Test for file exists?
Date: Mon, 20 Sep 2004 11:16:47 +0800
Message-ID: <002a01c49ec0$4527eb50$630736cb@athlon2000> (raw)
In-Reply-To: <m3llf5pvw3.fsf@wolfe.cbbrowne.com>

I want to use pgsql to send the command:

Copy sometable from 'sometable.csv';

But only if 'sometable.csv' exists;

If 'sometable.csv' does not exist as an input table I want to continue
the next command.

My full procedure is as follows and any help is greatly appreciated.

Thanks

Richard

CREATE OR REPLACE FUNCTION restore_database(text)
  RETURNS text AS
$BODY$
declare 
tblname record;
cnt record;
tname varchar :='';
tquery varchar :='';
filename varchar :='';

begin
tname := '';
for tblname in select tablename from pg_tables WHERE not(tablename like
'pg_%') and not(tablename like 't_%') 
and not(tablename like '%_list') order by tablename  loop
   raise notice '%',tblname.tablename;
   tquery := 'delete from ' || tblname.tablename ;
   execute tquery;
   filename := '/'||$1||'/'|| lower(tblname.tablename)||'.csv';
   tquery := 'copy ' || tblname.tablename || ' from ' ||
quote_literal(filename);
   execute tquery;
end loop;

return tquery;
end;
$BODY$
  LANGUAGE 'plpgsql' VOLATILE;

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.760 / Virus Database: 509 - Release Date: 10/09/2004
 




view thread (2+ messages)  latest in thread

Message-ID: <002a01c49ec0$4527eb50$630736cb@athlon2000>
Permalink:  ../002a01c49ec0$4527eb50$630736cb@athlon2000/
Also on:    postgresql.org/message-id/002a01c49ec0$4527eb50$630736cb@athlon2000

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: richard@ibisaustralia.com
  Subject: Re: Test for file exists?
  In-Reply-To: <002a01c49ec0$4527eb50$630736cb@athlon2000>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox