Re: trigger - dynamic WHERE clause

From: Tarlika Elisabeth Schmitz <postgresql3(at)numerixtechnology(dot)de>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: trigger - dynamic WHERE clause
Date: 2011-05-26 21:45:43
Message-ID: 20110526224543.3c71066e@dick.coachhouse
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sun, 22 May 2011 20:39:01 +0200
Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> wrote:

>Hello
>
>2011/5/22 Tarlika Elisabeth Schmitz <postgresql3(at)numerixtechnology(dot)de>:
>> EXECUTE 'SELECT 1 FROM ' || TG_TABLE_NAME || ' WHERE ' || whereclause
>> || ' FOR UPDATE;';
>>
>> I am generating the whereclause dynamically as the number of columns
>> queried varies.
>>
>> Am I right in assuming that I cannot use EXECUTE ... USING in this
>> scenario?
>>
>
>why not? You can use it - just USING has a fixed numbers of
>parameters, so you should to use a arrays.

Thank you for your responses, Pavel, and for your excellent blog pages.

Sorry, I am struggling with this a bit:

Currently, I am producing the whereclause on a subset of columns:

SELECT array_to_string (array(
SELECT newrecord.key || ' = ' || quote_literal(newrecord.value)
FROM (SELECT (each(hstore(NEW))).*) AS newrecord
WHERE newrecord.key LIKE 'id%' ), ' AND ')
INTO whereclause;

That gives me, for example:
SELECT 1 FROM test WHERE id1 = '26' AND id2 = 'name2' FOR UPDATE;

In an attempt to use EXECUTE '...' USING, I tried to execute
SELECT 1 FROM test WHERE id1 = $1 AND id2 = $2 FOR UPDATE;

I produced an array of corresponding values:
SELECT array(
SELECT newrecord.value
FROM (SELECT (each(hstore(NEW))).*) AS newrecord
WHERE newrecord.key LIKE 'id%'
) INTO av; -- text array

EXECUTE '...' USING av

==> ERROR: operator does not exist: integer = text[]

--

Best Regards,
Tarlika Elisabeth Schmitz

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Craig Ringer 2011-05-26 23:22:14 Re: max_connections proposal
Previous Message Bosco Rama 2011-05-26 21:18:41 Re: copy record?