Function with COPY command?

Started by Warren Bellalmost 19 years ago3 messagesgeneral
Jump to latest
#1Warren Bell
warren@clarksnutrition.com

Is there any way to make this function work?

CREATE OR REPLACE FUNCTION import_text_file(char(255)) RETURNS void AS $$
DECLARE
filename ALIAS FOR $1;
BEGIN
COPY table FROM filename;
END;
$$ LANGUAGE plpgsql;

The version below works fine, but I need something like the above version.

CREATE OR REPLACE FUNCTION import_text_file(char(255)) RETURNS void AS $$
DECLARE
filename ALIAS FOR $1;
BEGIN
COPY table FROM 'C:\\somefile.txt';
END;
$$ LANGUAGE plpgsql;

--
Thanks,

Warren

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Warren Bell (#1)
Re: Function with COPY command?

Warren <warren@clarksnutrition.com> writes:

Is there any way to make this function work?

CREATE OR REPLACE FUNCTION import_text_file(char(255)) RETURNS void AS $$
DECLARE
filename ALIAS FOR $1;
BEGIN
COPY table FROM filename;
END;
$$ LANGUAGE plpgsql;

Use EXECUTE.

regards, tom lane

#3Shoaib Mir
shoaibmir@gmail.com
In reply to: Tom Lane (#2)
Re: Function with COPY command?

Something like this will help:

CREATE OR REPLACE FUNCTION import_text_file(char(255)) RETURNS void AS $$
DECLARE
filename ALIAS FOR $1;
fin varchar;
BEGIN
fin := 'COPY table from ' || filename;
execute fin;
END;
$$ LANGUAGE plpgsql;

--
Shoaib Mir
EnterpriseDB (www.enterprisedb.com)

Show quoted text

On 6/14/07, Tom Lane <tgl@sss.pgh.pa.us> wrote:

Warren <warren@clarksnutrition.com> writes:

Is there any way to make this function work?

CREATE OR REPLACE FUNCTION import_text_file(char(255)) RETURNS void AS

$$

DECLARE
filename ALIAS FOR $1;
BEGIN
COPY table FROM filename;
END;
$$ LANGUAGE plpgsql;

Use EXECUTE.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster