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



-
Enviado a la lista de correo pgsql-es-ayuda ([email protected])
Para cambiar tu suscripción:
http://www.postgresql.org/mailpref/pgsql-es-ayuda

Responder a