Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XGVmM-0006qV-CP for pgsql-sql@arkaria.postgresql.org; Sun, 10 Aug 2014 16:15:30 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XGVmL-0002vi-B6 for pgsql-sql@arkaria.postgresql.org; Sun, 10 Aug 2014 16:15:29 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XGVmJ-0002vZ-4c for pgsql-sql@postgresql.org; Sun, 10 Aug 2014 16:15:27 +0000 Received: from ensieve.org ([54.195.250.169]) by magus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1XGVmE-00066r-Dt for pgsql-sql@postgresql.org; Sun, 10 Aug 2014 16:15:24 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=ensieve.org; s=alpha; h=Mime-Version:To:Date:Message-Id:Subject:Content-Transfer-Encoding:Content-Type:From; bh=HWcQC2U/8eqQLFVpvdL65UTe/ON9yyB0PV+Eapge4aw=; b=GnIhMVDe4eTxfhM4FuqYiXgZsWX+e/Efs7PG9/cR4DlYuFRqZNbTDLk0hBBBguwmgI9nCdDkv8tWywwCIbUcAXML96a/0eiC0bpFX6bk4LgMYn7Gfx2OHuFXB+4pJMWNntkvd2cCZyXNzr9QZyTE10xAFmwx4Z3yw19fQw/UOWU=; Received: from dhcp-90-212.inf.ed.ac.uk ([129.215.90.212]:56389) by ensieve.org with esmtpsa (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.82) (envelope-from ) id 1XGVld-0003Dl-79 for pgsql-sql@postgresql.org; Sun, 10 Aug 2014 16:14:45 +0000 From: pascal+postgres@ensieve.org Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Subject: Update Returning as subquery Message-Id: Date: Sun, 10 Aug 2014 17:15:20 +0100 To: pgsql-sql@postgresql.org Mime-Version: 1.0 (Mac OS X Mail 8.0 \(1971.5\)) X-Mailer: Apple Mail (2.1971.5) X-Pg-Spam-Score: -2.7 (--) 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 Hi, I want to update some values in a table, and need to count the number of va= lues actually changed; but ROW_COUNT returns the number of total rows touch= ed. But this gives a syntax error: SELECT count(*) INTO my_count FROM ( UPDATE stuff SET value =3D 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 va= lues like a SELECT statement would, and shouldn't it therefore be allowed t= o 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 =3D n.value FROM new_values AS n WHERE n.key =3D 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.=20 Cheers, --=20 Pascal Germroth --=20 Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql