Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TvWYp-0005yD-EF for pgsql-sql@arkaria.postgresql.org; Wed, 16 Jan 2013 17:13:59 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1TvWYo-00074o-En for pgsql-sql@arkaria.postgresql.org; Wed, 16 Jan 2013 17:13:58 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TvWYn-00074f-RA for pgsql-sql@postgresql.org; Wed, 16 Jan 2013 17:13:57 +0000 Received: from mail-ie0-f179.google.com ([209.85.223.179]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TvWYh-0002ll-36 for pgsql-sql@postgresql.org; Wed, 16 Jan 2013 17:13:57 +0000 Received: by mail-ie0-f179.google.com with SMTP id k14so2956213iea.24 for ; Wed, 16 Jan 2013 09:13:49 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:user-agent:date:subject:from:to:message-id:thread-topic :in-reply-to:mime-version:content-type:content-transfer-encoding :x-gm-message-state; bh=a+3r2G5hHKTZCzpSiOmDU8O49/JyHNhrpWMMwR23sbw=; b=mD7ULoFZ61wICRx3WrmAX0nxEQyMjmFrhWw2deaOnZOFaEgLgn2hC/zZdsjzYHpmcH /xY/bBXus0jCIn+FeoDDWtumS4wa7Pb37FQ5AgDnp2C+kwrwwasJak6u+c1l2WnH8LkZ 7PN3zavLgcZLHR8zarImYs7OUf1ZyxguyNLIoGJdOQ3jZUvMXrwfGOetbCxKceKdMjT3 t/rjgm5v0QgV3RgpnjfDt9d+h0R4DmFg2H8e7kPjXdxiZm1KgoDrB13A/z/Tu+9I3sCV 19/dKt/fcNdgrZB95lRTkavSMnRH+PrjAh1O2SwxMrLRZbGdDNH5wicoKxknDf5XwZsm WWkQ== X-Received: by 10.50.91.198 with SMTP id cg6mr5119353igb.102.1358356429024; Wed, 16 Jan 2013 09:13:49 -0800 (PST) Received: from [192.168.1.22] (adsl-074-245-040-156.sip.clt.bellsouth.net. [74.245.40.156]) by mx.google.com with ESMTPS id wo10sm3168937igc.13.2013.01.16.09.13.46 (version=TLSv1 cipher=RC4-SHA bits=128/128); Wed, 16 Jan 2013 09:13:47 -0800 (PST) User-Agent: Microsoft-MacOutlook/14.2.5.121010 Date: Wed, 16 Jan 2013 12:13:41 -0500 Subject: Re: returning the number of rows output by a copy command from a function From: James Sharrett To: Message-ID: Thread-Topic: [SQL] returning the number of rows output by a copy command from a function In-Reply-To: <50F6D6FF.8000504@gmail.com> Mime-version: 1.0 Content-type: text/plain; charset="US-ASCII" Content-transfer-encoding: 7bit X-Gm-Message-State: ALoCoQm5EVZUo9dGmthja4t9jLzlFkktqsQVNDZH9qLz20JfVF0GULlDf6cdD6PSTGHT/F+AZHZR 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 The # rows in the table <> # rows in the file because the table is grouped and aggregated so simple table row count wouldn't be accurate. The table can run in the 75M - 100M range so I was trying to avoid running all the aggregations once to output the file and then run the same code again just to get a count. On 1/16/13 11:36 AM, "Rob Sargent" wrote: >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 -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql