Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 5FACB2E0173 for ; Tue, 4 Mar 2008 21:03:01 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 88115-09 for ; Tue, 4 Mar 2008 21:02:58 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from gv-out-0910.google.com (gv-out-0910.google.com [216.239.58.187]) by postgresql.org (Postfix) with ESMTP id D3B522E0152 for ; Tue, 4 Mar 2008 21:02:57 -0400 (AST) Received: by gv-out-0910.google.com with SMTP id y18so979198gvf.7 for ; Tue, 04 Mar 2008 17:02:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; bh=1Oy3r3BHRVQMTLp9DCDwbDWuy42v7x0FJ2d3hMsUH54=; b=t1fSAW8liIFh/odhhUqUWwXUGrGVsfwd13CALZC0mLd7z4//Y3pGNxiCDJHqGFs3vqUHALcuAyjk2PNBfAAwcaurQAZHG3OoQkagk5SSF9qyHTQ9sRbij3j8MzLXK2mB7v1efd9Ef9mG9El4OB6w9lWIhhrKdBk/AwLKka4cyWY= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=oEDEcjf4JA33pebsqOeCQC2u9jA1C45rZPg9v83uWD7FGEshrRQaRJO95RUMqlGTRnhhhO+R/jcoFkqPupmHuJ58PfvVy9h0QwrAsmsd8KkcfzrkMOfEByxIDZgGMhNdXW/95uphUxJjR+H8CFO5bxLAlRsEX/DR/Sp4y3G78eM= Received: by 10.78.100.1 with SMTP id x1mr5008948hub.47.1204678975979; Tue, 04 Mar 2008 17:02:55 -0800 (PST) Received: by 10.78.150.19 with HTTP; Tue, 4 Mar 2008 17:02:55 -0800 (PST) Message-ID: <690707f60803041702j738a2b4et132d4ff2913ebecf@mail.gmail.com> Date: Tue, 4 Mar 2008 22:02:55 -0300 From: "Osvaldo Kussama" To: "Yura Gal" Subject: Re: using copy from in function Cc: pgsql-sql@postgresql.org In-Reply-To: <3b6c69d80803041451i6d4d358dk53e9b789bfacd40e@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <3b6c69d80803041451i6d4d358dk53e9b789bfacd40e@mail.gmail.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200803/14 X-Sequence-Number: 30342 2008/3/4, Yura Gal : > I'm trying to automate import data using CORY FROM. For this purpose I > wrote plpgsql function. As my postgres works on windows vista I need > to use E'' syntax for path-to-file. This syntax works fine in SQL > queries like: > COPY table FROM E'path_to_file_with_double_backslashes'; > > Following query works fine too: > COPY table FROM $$path_to_file_with_single_backslashes$$; > > However I can't figure it out how to use file_path variable as this > code throw error while compilation: > > CREATE OR REPLACE FUNCTION func (inout _chrom varchar) RETURNS varchar AS > $body$ > DECLARE > _chrom ALIAS FOR $1; > _file TEXT; > BEGIN > _file := $$c:\folder1\folder2\$$ || _chrom || '.txt'; > RAISE NOTICE 'Filename is -> %', _file; > COPY table (column) FROM _file CSV HEADER; > > statements; > > EXCEPTION > WHEN bad_copy_file_format THEN > _chrom := 'badformat'; > WHEN io_error THEN > _chrom := 'ioerr'; > WHEN undefined_file THEN > _chrom := 'unfile'; > RETURN; > END; > $body$ > LANGUAGE 'plpgsql' ; > > ERROR: syntax error in query at or near "$1" at character 35 > QUERY: COPY table (column) FROM $1 CSV HEADER > CONTEXT: SQL statement in PL/PgSQL function "func" near line 7 > > Is there any tricks to solve this problem? I suspect that something > wrong with quotation but can not find out a mistake. > > PS. Postgres server version is 8.3 > > Try: EXECUTE 'COPY table (column) FROM ' || _file || 'CSV HEADER;'; Osvaldo