You are on page 1of 1

1 --TRIGGERS, DISPARADORES O DESENCADENADORES

2
3
4 --1.- Implementacion de la funcion que se ejecutara de manera automatica
5 Create or replace function fn_actualiza_stock() returns trigger as
6 $$
7 Declare
8 Begin
9 update articulo set stock=stock-new.cantidad
10 where codigo_articulo=new.codigo_articulo;
11 return new;
12 end;
13 $$ language 'plpgsql'
14
15 ----
16 Create or replace function fn_actualiza_stock() returns trigger as
17 $$
18 Declare
19 Begin
20 IF(TG_OP='INSERT') THEN
21 update articulo set stock=stock-new.cantidad
22 where codigo_articulo=new.codigo_articulo;
23 return new;
24 END IF;
25 IF(TG_OP='DELETE') THEN
26 update articulo set stock=stock+old.cantidad
27 where codigo_articulo=old.codigo_articulo;
28 return old;
29 END IF;
30 end;
31 $$ language 'plpgsql'
32
33 ----
34 Select * from articulo order by 1
35 select * from venta_detalle
36
37 --2.- Crear el trigger y enlazarlo a la funcion
38 Create trigger tr_actualiza_stock after insert or delete ON venta_detalle
39 for each row execute procedure fn_actualiza_stock();
40
41 --drop trigger tr_actualiza_stock ON venta_detalle
42
43 Select * from articulo order by 1
44
45 delete from venta_detalle
46
47

You might also like