Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TvVyQ-0004eZ-14 for pgsql-sql@arkaria.postgresql.org; Wed, 16 Jan 2013 16:36:22 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1TvVyP-0007a7-CD for pgsql-sql@arkaria.postgresql.org; Wed, 16 Jan 2013 16:36:21 +0000 Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TvVyN-0007Yt-Uv for pgsql-sql@postgresql.org; Wed, 16 Jan 2013 16:36:20 +0000 Received: from mail-ie0-f182.google.com ([209.85.223.182]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TvVyM-0007PK-6J for pgsql-sql@postgresql.org; Wed, 16 Jan 2013 16:36:18 +0000 Received: by mail-ie0-f182.google.com with SMTP id s9so2872446iec.27 for ; Wed, 16 Jan 2013 08:36:17 -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:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=rjVxlidH1klvX0dzWOFFFFjmAy4BmOPd9htUX/rUV2g=; b=XPfYYTAylKt+1lm1aLhewEVwii9QsltuTbj+W1J/g0P7mVc8nog+kKpt3qanKucVyL 6oLb47JhFSGLlcVnUzo5qJ66u848Qz9gO0JvBM2aTlH19LD0NKggByYHGrf366K+gnyP xBdQAF/o+YLt2XD3zrHsGezGPjGBgeNqoY9M426N33py2YcX4xbCv00nDxOwA2b2QwyM HOz5S9FxcABJE30rnKbi6d0KLe/2yDnGMHHovLxkChlamFAcSfTSfaFQTc6yX3k4O5nw MW5N4ks3MSiBTfXI6ZQGcR1mlo1azONgcrvRDwsVtp/P16kdj8Wyj+vUAAHinquA1Nu6 96sA== X-Received: by 10.50.242.73 with SMTP id wo9mr5164290igc.36.1358354177356; Wed, 16 Jan 2013 08:36:17 -0800 (PST) Received: from [10.1.20.96] ([23.30.56.214]) by mx.google.com with ESMTPS id kp4sm5142580igc.1.2013.01.16.08.36.16 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 16 Jan 2013 08:36:16 -0800 (PST) Message-ID: <50F6D6FF.8000504@gmail.com> Date: Wed, 16 Jan 2013 09:36:15 -0700 From: Rob Sargent User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2 MIME-Version: 1.0 To: 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 09: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? > > > Thanks in advance, > James > declare export_count int; select count(*) from export_table into export_count(); raise notice 'Exported % rows', export_count; -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql