Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XGXap-00029a-CN for pgsql-sql@arkaria.postgresql.org; Sun, 10 Aug 2014 18:11:43 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XGXan-00015U-LY for pgsql-sql@arkaria.postgresql.org; Sun, 10 Aug 2014 18:11:41 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XGXal-00015L-H0 for pgsql-sql@postgresql.org; Sun, 10 Aug 2014 18:11:39 +0000 Received: from 216-139-250-139.aus.us.siteprotect.com ([216.139.250.139] helo=joe.nabble.com) by makus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1XGXah-00075v-Oi for pgsql-sql@postgresql.org; Sun, 10 Aug 2014 18:11:37 +0000 Received: from sam.nabble.com ([192.168.236.26]) by joe.nabble.com with esmtp (Exim 4.72) (envelope-from ) id 1XGXaH-00054L-17 for pgsql-sql@postgresql.org; Sun, 10 Aug 2014 11:11:09 -0700 Date: Sun, 10 Aug 2014 11:10:54 -0700 (PDT) From: David G Johnston To: pgsql-sql@postgresql.org Message-ID: <1407694253994-5814370.post@n5.nabble.com> In-Reply-To: References: Subject: Re: Update Returning as subquery MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: 1.8 (+) 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 pascal+postgres wrote > Hi, > > I want to update some values in a table, and need to count the number of > values actually changed; but ROW_COUNT returns the number of total rows > touched. > > But this gives a syntax error: > > SELECT count(*) INTO my_count > FROM ( > UPDATE stuff > SET value = maybe_null(key) > --^ > WHERE value IS NULL > RETURNING value ) AS t > WHERE value IS NOT NULL; > > Why is that forbidden? Isn't the purpose of a RETURNING clause to return > values like a SELECT statement would, and shouldn't it therefore be > allowed to occur in the same places? > > > > I switched it around using a CTE in this case: > > WITH new_values AS ( > SELECT key, maybe_null(key) AS value > FROM stuff WHERE value IS NULL) > UPDATE stuff AS s > SET value = n.value > FROM new_values AS n > WHERE n.key = s.key > AND n.value IS NOT NULL; > > Which only touches rows that will be changed and returns a useful > ROW_COUNT, but needs a join. > > Cheers, The following should work... WITH do_uodate AS ( UPDATE ... WHERE value IS NULL RETURNING value ) SELECT count(*) FROM do_update WHERE value IS NOT NULL I don't know why it doesn't work in subquery form but other than syntax this and your first form are equivalent. David J. -- View this message in context: http://postgresql.1045698.n5.nabble.com/Update-Returning-as-subquery-tp5814366p5814370.html Sent from the PostgreSQL - sql mailing list archive at Nabble.com. -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql