Creation of Trigger for INSERT in another table in Ibexpert

Asked

Viewed 834 times

0

Good morning, I had the need to create a stock control but I had never used Rigger.

I have 3 tables COMPRA ABASTECIMENTO and MOVIMENTO_ESTOQUE

Table MOVIMENTO_ESTOQUE :

inserir a descrição da imagem aqui

I would like when I make a purchase to record all data except the COD_ABASTECIMENTO and in the TIPO were recorded a C credit and when I make a supply be recorded all data less COD_COMPRA and COD_ITEMCOMPRA and in the TIPO were recorded a D debit, as I would create this Trigger for stock control ?

  • I’ll answer your question soon, but I’ll give you a warning: this would be better done with a view, so that it always sees the current state of the two tables.

1 answer

0


I’m assuming that the name of the fields are the same, and I left it empty in the code because if you use a counter, then you can call it. With this you should be able to do for the supply as well.

CREATE TRIGGER bi_movcompra for COMPRA
  BEFORE INSERT
AS
BEGIN
  INSERT INTO MOVIMENTO_ESTOQUE(CODIGO, DATA, COD_TANQUE, COD_PRODUTO, QUANTIDADE, TIPO, COD_COMPRA, COD_ITEMCOMPRA) VALUES(<COD>, NEW.DATA, NEW.COD_TANQUE, NEW.COD_PRODUTO, NEW.QUANTIDADE, 'C', NEW.COD_COMPRA, NEW.COD_ITEMCOMPRA)
END

Browser other questions tagged

You are not signed in. Login or sign up in order to post.