use two condition in a trigger [closed]
-
28-02-2021 - |
Question
I have 3 tables (shipment, Supplier, Part).
create table Shipment (
SID int,
PID int,
Qty int,
);
create table Supplier (
SID int,
Sname nvarchar(10),
Status int,
);
create table Shipment (
PID int,
Pname nvarchar(10),
city nvarchar(10),
);
I want to make this condition as a trigger to avoid the transaction. condition: prevent transactions over 10,000 qty by a supplier who has the status equal or bellow 30.
SELECT Shipment.Qty, Supplier.Status
FROM Shipment INNER JOIN
Supplier ON Shipment.SID = Supplier.SID
WHERE (Shipment.Qty >= 10000) AND (Supplier.Status <= 30)
I created something like this, but it doesn't work.
ALTER TRIGGER [dbo].[qytcontrolcheck]
ON [dbo].[Shipment]
AFTER INSERT,UPDATE
AS
BEGIN
if exists (SELECT Shipment.Qty, Supplier.Status
FROM Shipment INNER JOIN
Supplier ON Shipment.SID = Supplier.SID
WHERE (Shipment.Qty >= 10000) AND (Supplier.Status <= 30) )
BEGIN
print'NOT ENOUGH STATUS TO REGISTER THIS TRANSACTION'
rollback transaction;
END
END
what am I doing wrong? thank you in advance for helping me.
Solution
Replace , inside trigger code, the table Shipment
with Inserted
;
CREATE TRIGGER [dbo].[qytcontrolcheck]
ON [dbo].[Shipment]
AFTER INSERT,UPDATE
AS
BEGIN
if exists (SELECT Shipment.Qty, Supplier.Status
FROM Inserted as Shipment
INNER JOIN Supplier ON Shipment.SID = Supplier.SID
WHERE (Shipment.Qty >= 10000) AND (Supplier.Status <= 30) )
BEGIN
RAISERROR('NOT ENOUGH STATUS TO REGISTER THIS TRANSACTION',16,1);
rollback transaction;
END
END
dbfiddle here
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange