Sei sulla pagina 1di 2

create table ventas(

producto character varying(15) default '':: character varying,


valor numeric(7,2) default 0,
fecha date
);

create table ventas_ene() inherits (ventas);


create table ventas_feb() inherits (ventas);
create table ventas_mar() inherits (ventas);

alter table ventas_ene add constraint enero_vtas check (fecha >=


'2019-01-04'::date and fecha< '2019-01-31'::date);
alter table ventas_feb add constraint febrero_vtas check (fecha
>= '2019-02-04'::date and fecha< '2019-02-28'::date);
alter table ventas_mar add constraint marzo_vtas check (fecha
>= '2019-03-04'::date and fecha< '2019-03-31'::date);

create or replace function ventas_insertar()


returns trigger as $$
begin
if ( new.fecha >=date '2019-01-01' and
new.fecha <=date '2019-01-31') then
insert into ventas_ene values (new.*);
elseif (new.fecha >=date '2019-02-01' and
new.fecha <=date '2019-02-28')then
insert into ventas_feb values(new.*);
elseif (new.fecha >=date '2019-03-01' and
new.fecha <=date '2019-03-31')then
insert into ventas_mar values(new.*);
else
raise exception 'date out of range. Revise la funcion
ventas_insertar';
end if;
return null;
end;
$$ language plpgsql;

create trigger ventas_insertar_tg


before insert on ventas
for each row execute procedure ventas_insertar();
---#truncate table ventas cascade;
insert into ventas (producto, valor,
fecha)values('tramontina',1000,'2019-02-05');
insert into ventas (producto, valor, fecha)values('pil',200,'2019-
01-02');
insert into ventas (producto, valor, fecha)values('viva',10,'2019-
03-08');

Potrebbero piacerti anche