Re: help with dynamic table name - Mailing list pgsql-novice

From Tom Lane
Subject Re: help with dynamic table name
Date
Msg-id 26677.1173156529@sss.pgh.pa.us
Whole thread Raw
In response to Re: help with dynamic table name  ("Jasbinder Singh Bali" <jsbali@gmail.com>)
Responses Re: help with dynamic table name  ("Jasbinder Singh Bali" <jsbali@gmail.com>)
Re: help with dynamic table name  (Michael Fuhr <mike@fuhr.org>)
List pgsql-novice
"Jasbinder Singh Bali" <jsbali@gmail.com> writes:
> On 3/5/07, Peter Schonefeld <peter.schonefeld@gmail.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

pgsql-novice by date:

Previous
From: "Jasbinder Singh Bali"
Date:
Subject: Re: help with dynamic table name
Next
From: "Jasbinder Singh Bali"
Date:
Subject: Re: help with dynamic table name