Skip site navigation (1) Skip section navigation (2)

Re: help with dynamic table name

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Jasbinder Singh Bali" <jsbali(at)gmail(dot)com>
Cc: "Peter Schonefeld" <peter(dot)schonefeld(at)gmail(dot)com>, pgsql-novice(at)postgresql(dot)org
Subject: Re: help with dynamic table name
Date: 2007-03-06 04:48:49
Message-ID: 26677.1173156529@sss.pgh.pa.us (view raw or flat)
Thread:
Lists: pgsql-novice
"Jasbinder Singh Bali" <jsbali(at)gmail(dot)com> writes:
> On 3/5/07, Peter Schonefeld <peter(dot)schonefeld(at)gmail(dot)com> wrote:
>> sql := "INSERT INTO "+ $3 +" ('id','body') VALUES ("+ $1 +","+ $2 +")";
>> 
>> I get the error: "ERROR: 42703: column \"INSERT INTO \" does not exist"

> try replacing double quotes with single quotes and you should be fine i
> think

Actually, I think Peter's got the double and single quotes exactly
backwards.  You can't just randomly use one or the other: single quotes
surround literal strings, double quotes surround identifiers (and are
only really needed if the identifier wouldn't be syntactically an
identifier without them).  So Peter could write

  sql := 'INSERT INTO '+ $3 +' ("id","body") VALUES ('+ $1 +','+ $2 +')';

but in this particular case there's no point in double-quoting those
target-column names; it'd be the same to write

  sql := 'INSERT INTO '+ $3 +' (id,body) VALUES ('+ $1 +','+ $2 +')';

			regards, tom lane

In response to

Responses

pgsql-novice by date

Next:From: Jasbinder Singh BaliDate: 2007-03-06 04:49:50
Subject: Re: help with dynamic table name
Previous:From: Jasbinder Singh BaliDate: 2007-03-06 04:48:44
Subject: Re: help with dynamic table name

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group