Search code examples
sqltriggersproceduresql-server-2016create-table

How can I create a trigger that let me delete a row from a table and then update 2 tables


I want to create a trigger that let me delete a row from the table 'DETALLES' and after deleting that specific row I want to modify another table called "BOLETAS" and another called "ARTICULOS", the first table it's like this:

1

And for example I want to delete a row from this table, after that this second table called "ARTICULOS":

!2

must update his "STOCK" with the amount that is recovered because of the deleted row from the table "DETALLES" column "CANTIDAD" and finally the table called "BOLETAS":

3

must update the column "TOTAL" less the amount deleted from the table "DETALLES" column "IMPORTE".

So basically, there must be a Trigger that let me delete a row from the table "DETALLES" and update another 2 tables.

For example I decide to delete the second row from "DETALLES" and so the table "ARTICULOS" column "STOCK" of 'ART002' must be plus 1 because of the column "CANTIDAD" and the table "BOLETAS" must be less "60". I tried it for hours but I can't, Sorry for my bad english :( Those are the codes that I used to create the tables.

CREATE TABLE ARTICULOS(
CODART VARCHAR(6) PRIMARY KEY,
NOMART VARCHAR(50),
STOCK INT,
PRECIO NUMERIC(8,2))

CREATE TABLE INGRESOS(
CODART VARCHAR(6) ,
FECHA DATE,
CANTIDAD INT,
PRECIO NUMERIC(8,2),
CONSTRAINT  FK1  FOREIGN  KEY  (CODART)  REFERENCES ARTICULOS
)

CREATE TABLE 
CLIENTES(
CODCLI VARCHAR(6)  PRIMARY KEY,
NOMCLI  VARCHAR(50),
RUCCLI  INT,
DIRECCION  VARCHAR(50)
DEFAULT 'DESCONOCIDA'
)

CREATE TABLE BOLETAS(
NROBOL VARCHAR(10) PRIMARY KEY NONCLUSTERED,
CODCLI VARCHAR(6),
FECHA DATE,
TOTAL NUMERIC(8,2),
CONSTRAINT 
FK2   FOREIGN   KEY   (CODCLI)   REFERENCES CLIENTES
)

CREATE TABLE DETALLES(
NROBOL VARCHAR(10) ,
CODART VARCHAR(6),
CANTIDAD INT,
IMPORTE NUMERIC(8,2),
PRIMARY KEY NONCLUSTERED (NROBOL,CODART),
CONSTRAINT  FK3  FOREIGN 
KEY  (NROBOL)  REFERENCES 
BOLETAS,
CONSTRAINT  FK4  FOREIGN  KEY  (CODART)  REFERENCES ARTICULOS)

Solution

  • I played around with your tables and sample data. From the testing I did, it will work with only 1 row deleted or multiple rows deleted. You should do some more testing to verify, but I believe this should work with the data and logic you described.

    SET ANSI_NULLS ON
    GO
    
    SET QUOTED_IDENTIFIER ON
    GO
    
    CREATE TRIGGER [dbo].[rowUpdater]
       ON [dbo].[DETALLES]
       AFTER DELETE
    AS 
    BEGIN
        SET NOCOUNT ON;
    
        UPDATE ARTICULOS
        SET STOCK = a.STOCK + d.STOCK
        FROM ARTICULOS a
             JOIN (SELECT d.CODART,SUM(d.CANTIDAD) 'STOCK'
                    FROM deleted d
                    GROUP BY d.CODART) AS d ON a.CODART = d.CODART
    
        UPDATE BOLETAS
        SET TOTAL = b.TOTAL - d.TOTAL
        FROM BOLETAS b
             JOIN (SELECT d.NROBOL,SUM(d.IMPORTE) 'TOTAL'
                   FROM deleted d
                   GROUP BY d.NROBOL) AS d ON b.NROBOL = d.NROBOL
    END
    GO
    
    ALTER TABLE [dbo].[DETALLES] ENABLE TRIGGER [rowUpdater]
    GO