Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 809582E2E8B for ; Wed, 12 Mar 2008 13:25:26 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 57720-08 for ; Wed, 12 Mar 2008 13:25:17 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from perseverance.services.encs.concordia.ca (perseverance-96.encs.concordia.ca [132.205.96.94]) by postgresql.org (Postfix) with ESMTP id F0F352E0338 for ; Wed, 12 Mar 2008 13:25:16 -0300 (ADT) Received: from cacao.encs.concordia.ca (emilu@cacao.encs.concordia.ca [132.205.47.225]) by perseverance.services.encs.concordia.ca (envelope-from emilu@encs.concordia.ca) (8.13.7/8.13.7) with ESMTP id m2CGP5PF012702; Wed, 12 Mar 2008 12:25:05 -0400 Message-ID: <47D803E0.5050207@encs.concordia.ca> Date: Wed, 12 Mar 2008 12:25:04 -0400 From: Emi Lu Reply-To: emilu@encs.concordia.ca User-Agent: Thunderbird 2.0.0.12 (X11/20080213) MIME-Version: 1.0 To: Scott Marlowe CC: pgsql-sql@postgresql.org Subject: Re: case when... end in update clause? References: <47D7FB06.10707@encs.concordia.ca> <47D8009D.6000703@encs.concordia.ca> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.58 on perseverance.encs.concordia.ca at 2008/03/12 12:25:05 EDT X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200803/60 X-Sequence-Number: 30388 Hi Scott , >> No. I provide one small fake example. >> >> >> I want to know how to use case when in update/set clause as the following: >> >> update test >> set id = case when id = 5 then 6 else id end; > > Well, I think my point stands, that this stuff really belongs in a > where clause. The way you're doing it it updates ALL the rows whether > it needs to or not, my way only updates the rows that need it. How > about a REAL example of what you're trying to do. There may well be a > more efficient way of doing this than using a case statement. Or > not... Ok. The situation would like this, in one query: UPDATE tableName SET col1 = val1 when col1 satisfy condition1 col1 = val2 when col1 satisfy condition2 ... ... col1 = valN when col1 satisfy conditionN ... ... WHERE col3 satisfy conditionX;