Re: column name gets substitudes inside an execute statement of a trigger function. how to avoid it?!

From: Richard Huxton <dev(at)archonet(dot)com>
To: "G(dot) Allegri" <giohappy(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: column name gets substitudes inside an execute statement of a trigger function. how to avoid it?!
Date: 2009-06-09 15:04:26
Message-ID: 4A2E79FA.3020304@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

G. Allegri wrote:
>
> ERROR: Column 'lets_try' does not exist
> LINE 1: INSERT INTO seconda (nome) VALUES ('lets_try')
>
> My function and trigger are:
>
> CREATE OR REPLACE FUNCTION fun1() RETURNS TRIGGER AS
> $primaprova$
> DECLARE
> nome varchar;
> BEGIN
> IF (TG_OP='INSERT') THEN
> execute 'INSERT INTO ' || NEW.table || ' (nome) VALUES( "' || NEW.nome
> || '" );';

This will give VALUES ("lets_try") which is not a valid way to quote a
string. It *is* a valid way to quote an identifier (e.g. a column).

You probably want to use the quote_literal() function anyway to escape
any single-quotes in the value being quoted.

--
Richard Huxton
Archonet Ltd

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message G. Allegri 2009-06-09 15:07:19 Re: column name gets substitudes inside an execute statement of a trigger function. how to avoid it?!
Previous Message Bill Moran 2009-06-09 15:04:10 Re: postgres getting slow under heavy load though autivacuum is enabled