Search Postgresql Archives

Re: Fwd: Trigger on VIEW not firing

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi again,

IIUC you want to update the alarm table only when the speed limit is above 100. You cannot achieve it by the view and triggers you have written here because the trigger will be fired even for values < 100

=# INSERT INTO speedv VALUES (1, 'test', 10);
INSERT 0 1

=# SELECT * FROM speedv;
 id | type | speed 
----+------+-------
(0 rows)

=# SELECT * FROM car;
 id | type | speed 
----+------+-------
(0 rows)

=# SELECT * FROM alarm;
 name | id | type  |            init            | fired | t_end | t_user 
------+----+-------+----------------------------+-------+-------+--------
 test |  0 | SPEED | 2013-07-30 18:08:01.006979 |       |       | 
 test |  0 | SPEED | 2013-07-30 18:20:00.73507  |       |       | 
(2 rows)

If you want to update the alarm table for speed > 100 then use an if else clause in the trigger function:

CREATE OR REPLACE FUNCTION update_alarm_view() RETURNS TRIGGER AS $alarm_tg$
    BEGIN
        IF (new.speed > 100) THEN
               IF (TG_OP = 'UPDATE') THEN
                   INSERT INTO alarm  VALUES(NEW.type, 0,'SPEED',now(),NULL,NULL,'');
               ELSEIF (TG_OP = 'INSERT') THEN
                    INSERT INTO alarm VALUES(NEW.type, 0,'SPEED',now(),NULL,NULL,'');
                END IF;
        END IF;
        RETURN new;
    END;
$alarm_tg$ LANGUAGE plpgsql;

And write the trigger on the car table. 



-- 

Beena Emerson


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux