Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
create table instock
(
storeid smallint not null,
wineid smallint not null,
quantity smallint not null,
primary key (storeid, wineid),
foreign key (storeid) references stores,
foreign key (wineid) references wines
)
create table bigstores
(
storeid smallint not null,
city char(20) not null,
phone char(10) not null,
primary key (storeid)
)
IF EXISTS ( SELECT Name FROM sysobjects
WHERE Name = 'TRI_deleteINSTOCK' AND Type = 'P' )
DROP trigger TRI_DELETEINSTOCK
GO
CREATE TRIGGER TRI_deleteINSTOCK
ON instock
FOR delete
AS
BEGIN
DECLARE
@STOREID SMALLINT,
@WINEID SMALLINT,
@TOTALQUANTITY INT
SELECT @STOREID = d.STOREID FROM deleted d
SELECT @wineid = d.wineid from deleted d

SELECT @TOTALQUANTITY = SUM(QUANTITY)
FROM INSTOCK
WHERE STOREID =@STOREID

DELETE FROM INSTOCK
WHERE storeid =@STOREID AND wineid =@WINEID
IF NOT EXISTS(SELECT storeid FROM bigstores WHERE storeid =@storeid )
BEGIN
DELETE FROM bigstores
where StoreId = @StoreId AND @totalquantity <= 50000
END
END
Posted
Comments
Deepu S Nair 21-Jan-15 2:29am    
What you tried so far?
Member 11387088 21-Jan-15 2:32am    
i have created a delete trigger and if i delete values in one table means it should be fired in another table and i've mentioned the quantity if the quantity is lesser than 50000 means it willnot be triggered
Sinisa Hajnal 21-Jan-15 2:45am    
The trigger never "fires on another table" - trigger by definition reacts to events on the table it is defined on. However, you can delete from other tables. What is the problem? Do you get an error from your trigger or it behaves unexpectedly?

1 solution

IF EXISTS ( SELECT Name FROM sysobjects
WHERE Name = 'TRI_deleteINSTOCK' AND Type = 'P' )
DROP trigger TRI_DELETEINSTOCK
GO
CREATE TRIGGER TRI_deleteINSTOCK
ON instock
FOR delete
AS
BEGIN
DECLARE
@STOREID SMALLINT,
@WINEID SMALLINT,
@TOTALQUANTITY INT
SELECT @STOREID = d.STOREID FROM deleted d
SELECT @wineid = d.wineid from deleted d

SELECT @TOTALQUANTITY = SUM(QUANTITY)
FROM INSTOCK
WHERE STOREID =@STOREID

DELETE FROM bigstores
WHERE storeid =@STOREID
IF NOT EXISTS(SELECT storeid FROM bigstores WHERE storeid =@storeid )
BEGIN
DELETE FROM bigstores
where StoreId = @StoreId AND @totalquantity <= 50000
END
END
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900