Re: syntax prob

From: Tod McQuillin <devin(at)spamcop(dot)net>
To: postgresql <pgsql(at)symcom(dot)com>
Cc: PgSQL-SQL <pgsql-sql(at)postgresql(dot)org>
Subject: Re: syntax prob
Date: 2001-02-24 15:59:12
Message-ID: Pine.GSO.4.31.0102240950200.13223-100000@sysadmin
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Ken Kline 2001-02-24 19:11:28 Re: greetings
Previous Message Olaf Marc Zanger 2001-02-24 15:55:40 a tricky one