From: | Henrry Joshney Servitá Sánchez <henser05(at)yahoo(dot)com> |
---|---|
To: | pgsql-es-ayuda(at)postgresql(dot)org |
Subject: | ayuda TRIGGER |
Date: | 2010-06-10 03:59:56 |
Message-ID: | 751957.6661.qm@web53507.mail.re2.yahoo.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-es-ayuda |
buenas tengo las siguientes tablas.
CREATE TABLE item
(
item_id serial NOT NULL,
description character varying(64) NOT NULL,
cost_price numeric(7,2),
sell_price numeric(7,2),
CONSTRAINT item_pk PRIMARY KEY (item_id)
)
WITH (
OIDS=FALSE
);
CREATE TABLE stock
(
item_id integer NOT NULL,
quantity integer NOT NULL,
CONSTRAINT stock_pk PRIMARY KEY (item_id)
)
WITH (
OIDS=FALSE
);
estoy creando un trigger que me controle al momento de insertar un nuevo registro en la tabla item se inserte otro registro en la tabla stock con el campo item_id de la tabla iten y el quantity con el valor 0.
esta es la funcion que cree
CREATE OR REPLACE FUNCTION nuevo_producto() RETURNS trigger AS
$BODY$
declare
codigo int;
BEGIN
if (TG_OP='INSERT' ) THEN
IF (NEW.item_id != NEW.item_id) then
INSERT INTO item VALUES (item_id, description,cost_price,sell_price);
insert into stock values(item.item_id,0);
ELSE
RAISE NOTICE 'fallo';--, quantity; -- Prints 30
END IF;
end if;
RETURN null;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE;
create trigger insertar_producto
after insert on item
for each row
execute procedure nuevo_producto();
espero que haya sido claro y puedan ayudarme muchas gracias.
saludos
From | Date | Subject | |
---|---|---|---|
Next Message | Alvaro Herrera | 2010-06-10 04:18:06 | Re: ayuda TRIGGER |
Previous Message | Ernesto Herrera | 2010-06-10 02:27:13 | ayuda sobre SRF |