How to insert a Trigger Insert in the Stock Table after Insert in the Product Table on IBM DB2

Asked

Viewed 453 times

1

I am trying to learn the basics of DB2 and modeled a database in which I need to insert in the table "stock" a default value for each product registered in the table "product" as cut below. This insertion is performed by a Trigger that works with proper syntax in both MYSQL and Oracle. On IBM DB2 V11 EXPRESS-C, however, I can’t even insert Trigger because I get the message "Failed queries => 4". I checked some examples of triggers for DB2 and can’t figure out what mistake I’m making.

Below is a cut-out of the tables, the Trigger and the system response

- -----------------------------------------------------
-- Table lojamodel.produto
-- -----------------------------------------------------
CREATE TABLE produto (
  idProduto INT NOT NULL PRIMARY KEY
  GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1),
  prod_nome VARCHAR(45) NOT NULL,
  prod_descricao VARCHAR(60) NOT NULL,
  prod_grupo VARCHAR(15) NOT NULL,
  prod_codigo13d VARCHAR(13) NOT NULL,
  prod_codloja VARCHAR(5) NOT NULL,
  prod_fornecid INT NOT NULL,
  FOREIGN KEY (prod_fornecid)
    REFERENCES fornecedor (idFornecedor)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);

-- Produtos 
insert into produto (prod_nome, prod_descricao, prod_grupo, prod_codigo13d, prod_codloja, prod_fornecid) values('Notebook ABCERF M15', 'Notebook 16G 2T I7 15.6 ', 'Computadores', '7891000001311', '1311', 1);
insert into produto (prod_nome, prod_descricao, prod_grupo, prod_codigo13d, prod_codloja, prod_fornecid) values('Notebook ABCERF M2 S14', 'Notebook 8G 1T I5 14', 'Computadores', '7891000001312', '1312', 1);  
insert into produto (prod_nome, prod_descricao, prod_grupo, prod_codigo13d, prod_codloja, prod_fornecid) values('Monitor AFODCS 24', 'Monitor 24" LED', 'Monitores', '7891000001321', '1321', 2);
insert into produto (prod_nome, prod_descricao, prod_grupo, prod_codigo13d, prod_codloja, prod_fornecid) values('Monitor AFODCS21', 'Monitor 21,5" LED', 'Monitores', '7891000001322', '1322', 2);



CREATE TABLE estoque (
  idestoque INT NOT NULL PRIMARY KEY
  GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1),
  est_quantidade INT NOT NULL DEFAULT '0',
  est_precovenda FLOAT NOT NULL DEFAULT '0.00',
  est_minquant INT NOT NULL DEFAULT '1',
  est_prodid INT NOT NULL,
  FOREIGN KEY (est_prodid)
    REFERENCES produto (idProduto)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);


-- Meu  Trigger 

CREATE OR REPLACE TRIGGER insere_estoque  
AFTER INSERT ON produto  
FOR EACH ROW MODE DB2SQL
BEGIN
  declare v_codigo int default 0;
  set v_codigo = (select max(idproduto) from produto);
  insert into estoque (est_quantidade, est_precovenda, est_minquant, est_prodid) values (0, 0.00, 2, v_codigo);
END@


--  DB2 Resultado de SQL 
Script2.sql

CREATE TRIGGER insere_estoque
AFTER INSERT ON produto
FOR EACH ROW MODE DB2SQL

BEGIN
declare v_codigo int default 0
set v_codigo = (select max(idproduto) from produto)
insert into estoque (est_quantidade, est_precovenda, est_minquant, est_prodid) values (0, 0, 2, v_codigo)
END

Failed queries => 4

Total Execution time => 0 ms

No answers

Browser other questions tagged

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