Re: [GENERAL] Trouble with plpgsql generic trigger function using

Поиск
Список
Период
Сортировка
От Richard Huxton
Тема Re: [GENERAL] Trouble with plpgsql generic trigger function using
Дата
Msg-id 45485BB2.6040208@archonet.com
обсуждение исходный текст
Ответ на Trouble with plpgsql generic trigger function using special variables  ("Lenorovitz, Joel" <Joel.Lenorovitz@usap.gov>)
Список pgsql-novice
Lenorovitz, Joel wrote:
> I'd like to create a trigger function whose use can extend to multiple
> tables by employing the special variables available (e.g., TG_RELNAME).
[snip]
> Any advice
> on outputting the values of the variables to the console for inspection
> during testing would be welcome as well (RAISE EXCEPTION doesn't allow a
> variable value in the message string, plus it seems a little harsh).

You can embed variables into RAISEd messages. You'd normally use RAISE
NOTICE for this sort of thing.
   RAISE <LEVEL> 'My variables % and %', var1, var2;

> CREATE OR REPLACE FUNCTION trigger_fxn() RETURNS TRIGGER AS $$
>     BEGIN
>         IF ((TG_OP = 'INSERT') AND (TG_WHEN = 'BEFORE')) THEN
>             IF (SELECT COUNT(*) FROM text(TG_RELNAME)) < 4

This won't work, because plpgsql pre-plans queries. You'll need to use
the EXECUTE facility:
   EXECUTE 'SELECT COUNT(*) FROM ' || TG_RELNAME;
You'll want the FOR ... IN ... EXECUTE form to read a value into a
variable. See "Looping through query results" in the manual for details.

Some of the other procedural languages treat queries as text anyway, so
they'll let you do what you're trying.

--
   Richard Huxton
   Archonet Ltd

В списке pgsql-novice по дате отправления:

Предыдущее
От: "shaik mastan dilshad"
Дата:
Сообщение: Re: [ADMIN] Copying data from table to table (cloned tables)
Следующее
От: Alban Hertroys
Дата:
Сообщение: Re: [GENERAL] Trouble with plpgsql generic trigger function using