Гость
Целевая тема:
Создать новую тему:
Автор:
Форумы / PostgreSQL [игнор отключен] [закрыт для гостей] / кто вызвал триггер / 4 сообщений из 4, страница 1 из 1
28.07.2006, 08:44
    #33884027
NikulinAS
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
кто вызвал триггер
Как в триггере узнать на какое событие он был вызван и с какой таблицей это действие было сделано ?
...
Рейтинг: 0 / 0
28.07.2006, 10:26
    #33884319
domanix
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
кто вызвал триггер
Цитата из документации...( которую иногда нужно более подробно изучать, прежде чем задавать вопросы..)
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
When a PL/pgSQL function is called as a trigger, several special variables are 
created automatically in the top-level block. They are: 
NEW 
Data type RECORD; variable holding the new database row for INSERT/UPDATE 
operations in row-level triggers. This variable is NULL in statement-level triggers. 
OLD 
Data type RECORD; variable holding the old database row for UPDATE/DELETE 
operations in row-level triggers. This variable is NULL in statement-level triggers. 
TG_NAME 
Data type name; variable that contains the name of the trigger actually fired. 
TG_WHEN 
Data type text; a string of either BEFORE or AFTER depending on the trigger's 
definition. 
TG_LEVEL 
Data type text; a string of either ROW or STATEMENT depending on the trigger's 
definition. 
TG_OP 
Data type text; a string of INSERT, UPDATE, or DELETE telling for which operation the 
trigger was fired. 
TG_RELID 
Data type oid; the object ID of the table that caused the trigger invocation. 
TG_RELNAME 
Data type name; the name of the table that caused the trigger invocation. 
TG_NARGS 
Data type integer; the number of arguments given to the trigger procedure in the 
CREATE TRIGGER statement. 
TG_ARGV[] 
Data type array of text; the arguments from the CREATE TRIGGER statement. The 
index counts from 0. Invalid indices (less than 0 or greater than or equal to tg_nargs) 
result in a null value. 
...
Рейтинг: 0 / 0
28.07.2006, 10:26
    #33884320
pamir
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
кто вызвал триггер
...
Рейтинг: 0 / 0
28.07.2006, 10:27
    #33884326
domanix
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
кто вызвал триггер
Ну и пример в догонку....

Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
CREATE TABLE emp (
    empname text,
    salary integer,
    last_date timestamp,
    last_user text
);

CREATE FUNCTION emp_stamp() RETURNS trigger AS $emp_stamp$
    BEGIN
        -- Check that empname and salary are given
        IF NEW.empname IS NULL THEN
            RAISE EXCEPTION 'empname cannot be null';
        END IF;
        IF NEW.salary IS NULL THEN
            RAISE EXCEPTION '% cannot have null salary', NEW.empname;
        END IF;

        -- Who works for us when she must pay for it?
        IF NEW.salary <  0  THEN
            RAISE EXCEPTION '% cannot have a negative salary', NEW.empname;
        END IF;

        -- Remember who changed the payroll when
        NEW.last_date := current_timestamp;
        NEW.last_user := current_user;
        RETURN NEW;
    END;
$emp_stamp$ LANGUAGE plpgsql;

CREATE TRIGGER emp_stamp BEFORE INSERT OR UPDATE ON emp
    FOR EACH ROW EXECUTE PROCEDURE emp_stamp();
...
Рейтинг: 0 / 0
Форумы / PostgreSQL [игнор отключен] [закрыт для гостей] / кто вызвал триггер / 4 сообщений из 4, страница 1 из 1
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]