pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tod McQuillin <devin@spamcop.net>
To: postgresql <pgsql@symcom.com>
Cc: PgSQL-SQL <pgsql-sql@postgresql.org>
Subject: Re: syntax prob
Date: Sat, 24 Feb 2001 09:59:12 -0600 (CST)
Message-ID: <Pine.GSO.4.31.0102240950200.13223-100000@sysadmin> (raw)
In-Reply-To: <200102240217.f1O2Hhx35303@mail.postgresql.org>

On Fri, 23 Feb 2001, postgresql wrote:

> when doing  updates of multiple fields there commas between
> the  elements?

Yes.  update t set a=b, c=d where ...

> if I could get to the server I would just try it. what happens if you have
> only 1 field to update and you add a comma, like this
>
> update table set cname = 'Bill',  where acode = 'AVAN';

You get: ERROR:  parser: parse error at or near "where"

> I am trying to concatenate an update string and I would love to not
> have to worry about the comma.

Here's what I do.  I keep a list of the things I am updating, like this,
in perl:

push(@updates, "set a=b");
push(@updates, "set c=d");
$sql = "update t " . join(", ", @updates) . " where ...";

or like this in php:

$updates[] = "set a=b";
$updates[] = "set c=d";
$sql = "update t " . implode(", ", $updates) . " where ...";

The join() and implode() functions make sure no comma is used if the
updates array has fewer than two elements.  Otherwise they stick commas
between each one, just like sql wants.
-- 
Tod McQuillin





view thread (2+ messages)

Message-ID: <Pine.GSO.4.31.0102240950200.13223-100000@sysadmin>
Permalink:  ../Pine.GSO.4.31.0102240950200.13223-100000@sysadmin/
Also on:    postgresql.org/message-id/Pine.GSO.4.31.0102240950200.13223-100000@sysadmin

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: devin@spamcop.net, pgsql@symcom.com
  Subject: Re: syntax prob
  In-Reply-To: <Pine.GSO.4.31.0102240950200.13223-100000@sysadmin>
  References: <200102240217.f1O2Hhx35303@mail.postgresql.org> <Pine.GSO.4.31.0102240950200.13223-100000@sysadmin>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox