Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id C17E42E1275 for ; Wed, 12 Mar 2008 13:19:39 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 55676-04 for ; Wed, 12 Mar 2008 13:19:16 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from rn-out-0910.google.com (rn-out-0910.google.com [64.233.170.191]) by postgresql.org (Postfix) with ESMTP id 525702E0106 for ; Wed, 12 Mar 2008 13:19:17 -0300 (ADT) Received: by rn-out-0910.google.com with SMTP id e11so1763841rng.17 for ; Wed, 12 Mar 2008 09:19:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; bh=NEmS1Y+9Rw7uiMF1bHsxcIZzjPX6V7Mfq0nyqeUFGwk=; b=m/4AQ4Sj1esVeRbxLkaQWGqX6EB24uCKAFtZwnOVHTMwuCKCnRjbU6qjNjE6r5/VH/Uq9Vi8dB188fsas/CJzWYArrHLl4gOVEztE5fW11PWmssn3hnyiC5ye+tD4QROT0AZ/kfgZAOrWfT5uNaVbjH503+tyxp4ntbjaWcl7R8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=sFepiwTzrtx2UyEAvtb7VoCfmj9LjNYuXKDNN3+KW0PYrKquN6zQR5YREnxsvEEDSL+PV/tP/Gry+Sx4Z7BzKgUbyblHBQlUVrducoqrFn5oE6heHrH2MCWr+8obDfgshO1/ohv9Ll8Qs4DeyMkuEBKHxs7ECceHeyDO7gvLCw4= Received: by 10.114.173.15 with SMTP id v15mr7288814wae.63.1205338756013; Wed, 12 Mar 2008 09:19:16 -0700 (PDT) Received: by 10.114.133.6 with HTTP; Wed, 12 Mar 2008 09:19:15 -0700 (PDT) Message-ID: Date: Wed, 12 Mar 2008 09:19:15 -0700 From: "Scott Marlowe" To: emilu@encs.concordia.ca Subject: Re: case when... end in update clause? Cc: pgsql-sql@postgresql.org In-Reply-To: <47D8009D.6000703@encs.concordia.ca> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <47D7FB06.10707@encs.concordia.ca> <47D8009D.6000703@encs.concordia.ca> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200803/59 X-Sequence-Number: 30387 On Wed, Mar 12, 2008 at 9:11 AM, Emi Lu wrote: > >> > >> I use one small Example, table: test > >> ============================= > >> id > >> == > >> 5 > >> 6 > >> 8 > >> > >> try to update test.id > >> > >> > >> update test > >> > >> case > >> when id =5 then SET id = 6 > >> end > >> ; > > > > would this work: > > > > update test set id=5 where id=6; > > > > 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...