Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TvW4i-0004qj-32 for pgsql-sql@arkaria.postgresql.org; Wed, 16 Jan 2013 16:42:52 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1TvW4h-0008Ra-4t for pgsql-sql@arkaria.postgresql.org; Wed, 16 Jan 2013 16:42:51 +0000 Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TvW4g-0008RR-33 for pgsql-sql@postgresql.org; Wed, 16 Jan 2013 16:42:50 +0000 Received: from mail-pa0-f46.google.com ([209.85.220.46]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TvW4d-0007Tt-Sc for pgsql-sql@postgresql.org; Wed, 16 Jan 2013 16:42:48 +0000 Received: by mail-pa0-f46.google.com with SMTP id bh2so881534pad.33 for ; Wed, 16 Jan 2013 08:42:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:message-id:date:from:user-agent:mime-version:to:cc :subject:references:in-reply-to:content-type :content-transfer-encoding; bh=+ch/jSxWyz5CvVKzcAUu8Tjg6YvBKSlkK/5ZY4yrThM=; b=0EkmBgYNQLhnBF8h6SUBa+r1xsxpBa250ANUxxXDd70wetaYixsE1XBB7KUF0cv3CT mUDRy2liLvmpRvwHuBHPHwqpJj8bUXTX9W3hg25fBaJF/vnqz4WCY8j+q1ZY2pQb7mFW NiQP3N2Ff7A5SKwwvRUw5DnUdw6TTPG8/1k2fwSefwhaV67GdpwwCwBmNBIcAno3mQNY hK1g8hTpulxGAZElVQhRfcLPtm3ga7lttfga9wJJipHIETbeTIt8n/cilsjHLa/GZOx8 fZ8ZHtsrDm45KCqmL+yEkzCk0W8+JybLLcx/w1U5rhHwALDvYM+Xx5e/j7cFpnA5fGrL jmzA== X-Received: by 10.68.238.39 with SMTP id vh7mr4593467pbc.6.1358354566120; Wed, 16 Jan 2013 08:42:46 -0800 (PST) Received: from [192.168.200.187] ([199.48.193.159]) by mx.google.com with ESMTPS id jv1sm12565217pbc.36.2013.01.16.08.42.44 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 16 Jan 2013 08:42:45 -0800 (PST) Message-ID: <50F6D883.50901@gmail.com> Date: Wed, 16 Jan 2013 08:42:43 -0800 From: Adrian Klaver User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130107 Thunderbird/17.0.2 MIME-Version: 1.0 To: James Sharrett CC: pgsql-sql@postgresql.org Subject: Re: returning the number of rows output by a copy command from a function References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org On 01/16/2013 08:30 AM, James Sharrett wrote: > I have a function that generates a table of records and then a SQL > statement that does a COPY into a text file. I want to return the > number of records output into the text file from my function. The > number of rows in the table is not necessarily the number of rows in the > file due to summarization of data in the table on the way out. Here is > a very shortened version of what I'm doing: > > > CREATE OR REPLACE FUNCTION export_data(list of parameters) > RETURNS integer AS > $BODY$ > > declare > My variables > > Begin > > { A lot of SQL to build and populate the table of records to export} > > > strSQL := 'copy (select MyColumns from MyExportTable) to MyFile.csv with > CSV HEADER;'; > Execute strSQL; > > Return 0; > > end > $BODY$ > LANGUAGE plpgsql VOLATILE > > strSQL gets dynamically generated so it's not a static statement. > > This all works exactly as I want. But when I try to get the row count > back out I cannot get it. I've tried the following: > > 1. > strSQL := 'copy (select MyColumns from MyExportTable) to MyFile.csv with > CSV HEADER;'; > Execute strSQL into export_count; > > Return export_count; > > This give me an error saying that I've tried to use the INTO statement > with a command that doesn't return data. > > > 2. > strSQL := 'copy (select MyColumns from MyExportTable) to MyFile.csv with > CSV HEADER;'; > Execute strSQL; > > Get diagnostics export_count = row_count; > > This always returns zero. > > 3. > strSQL := 'copy (select MyColumns from MyExportTable) to MyFile.csv with > CSV HEADER;'; > Execute strSQL; > > Return row_count; > > This returns a null. > > Any way to do this? If it helps: http://www.postgresql.org/docs/9.2/interactive/sql-copy.html " On successful completion, a COPY command returns a command tag of the form COPY count The count is the number of rows copied. " So it looks like you will need to parse the string for the count. > > > Thanks in advance, > James > -- Adrian Klaver adrian.klaver@gmail.com -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql