Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TvWb5-00067H-Bi for pgsql-sql@arkaria.postgresql.org; Wed, 16 Jan 2013 17:16:19 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1TvWb4-0000H9-SH for pgsql-sql@arkaria.postgresql.org; Wed, 16 Jan 2013 17:16:18 +0000 Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TvWb4-0000H4-4r for pgsql-sql@postgresql.org; Wed, 16 Jan 2013 17:16:18 +0000 Received: from mail-ie0-f170.google.com ([209.85.223.170]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TvWb2-0007yi-HH for pgsql-sql@postgresql.org; Wed, 16 Jan 2013 17:16:17 +0000 Received: by mail-ie0-f170.google.com with SMTP id k10so2978199iea.15 for ; Wed, 16 Jan 2013 09:16:15 -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=E4vxcJTHFDgGS5u45pdclUqi4sZoRXoklktqDAEof1o=; b=c/wv65GBJCI9k3qwRrBFH66fhvXjNVjV1pcuE4Op98+qFMIVpVivGCu8TGe3LXyBXo CJAi4AKWJp3mVDeb2T15JPFQu8CHFvh84IfcAR2m6KeMg5LJPNqrfQc45ji9QYrATKVi D6tAKuqs9kv1zYosGE+cBo/sSdTi0HYsLm7ffq151SMaTb4nX5H5RlKhNXRwBVqZbOyZ Q9Lc1G9PUrD/GXOlmw61mpsOAmf5Z1bxBo/GGYQCPL/X883y75ohg4xWisTTj+q2Hexg IviyNVNp8MCZjZ7Cbpz5dNzfQ2AMK1aAMtZQ76h8JJO0RZ4K2ctjq7reYtgpijXC232O 1y3A== X-Received: by 10.42.27.74 with SMTP id i10mr1131558icc.47.1358356575310; Wed, 16 Jan 2013 09:16:15 -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 bh3sm4793609igc.0.2013.01.16.09.16.11 (version=TLSv1 cipher=RC4-SHA bits=128/128); Wed, 16 Jan 2013 09:16:14 -0800 (PST) User-Agent: Microsoft-MacOutlook/14.2.5.121010 Date: Wed, 16 Jan 2013 12:16:08 -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: <50F6D883.50901@gmail.com> Mime-version: 1.0 Content-type: text/plain; charset="US-ASCII" Content-transfer-encoding: 7bit X-Gm-Message-State: ALoCoQnb+eecRihf7dxgLQh6oTmyDmXWG1EYmpgrc0bU0GNkv6k6HpUYwZYc0Ym1e+BC4PGVcGfc 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 problem I have is that I get nothing back when the COPY is run inside the function other than what I explicitly return from the function so I don't have anything to parse. It's odd that the record count in the function is treated differently than from sql query in GET DIAGNOSTIC since the format and information in the string (when run outside of the function) are exactly the same. On 1/16/13 11:42 AM, "Adrian Klaver" wrote: >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