Re: frustrated by plpgsql procedure

From: John DeSoi <desoi(at)pgedit(dot)com>
To: Dino Vliet <dino_vliet(at)yahoo(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: frustrated by plpgsql procedure
Date: 2006-03-27 19:43:07
Message-ID: AEBB6D62-FAE7-4BE1-BFBA-7AD74622D18E@pgedit.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On Mar 27, 2006, at 2:10 PM, Dino Vliet wrote:

> Can somebody tell me why my location variable is NOT
> working as expected? I would like to use it in a loop
> to create multiple text files which names would be
> different because of the way I concatenate it with the
> looping variable.

You can't just stick an arbitrary string in the middle of a SQL
statement. You can build a SQL statement and then run it with EXECUTE.

Try something like this:

create or replace function doedit() returns varchar AS $$
/* Procedure to create textfile from database table.
*/

DECLARE
i integer := 340;
start date :='2004-08-06';
eind date :='2004-08-12';
location varchar(30) :='/usr/Data/plpgtrainin';

BEGIN
create table cancel as (SOME QUERY);
location := location || i || '.txt' ::varchar(30);
raise notice 'location is here %', location;
execute 'copy cancel to ' || location || ' with delimiter as \',\'
null as \'.\'';
return location;
END;
$$ Language plpgsql;

Also note you must have super user access to use COPY, so it still
might fail if you don't have the right privileges.

John DeSoi, Ph.D.
http://pgedit.com/
Power Tools for PostgreSQL

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ted Byers 2006-03-27 19:53:36 Re: [Bulk] General advice on database/web applications
Previous Message Tom Lane 2006-03-27 19:28:05 Re: frustrated by plpgsql procedure