Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 CREATE AFTER INSERT TRIGGER

Author  Topic 

Sql_question
Starting Member

2 Posts

Posted - 2010-02-22 : 18:53:29
Hello, im new to sql and im struggeling with my trigger query.
There are 3 tables: -orders,
-parts and
-suppliers

I have to write an AFTER INSERT Trigger on the orders table to verify that the supplier and the part specified in an insert statement are in the same city. Then i have to reject all orders which do not have a supplier and part in the same city and print a message to the screen with the reason for rejecting the order. For valid orders, i have to print a message to the screen indicating that the order was valid.

I made this so far, but when i try to insert data in the orders table they all get rejected by the trigger. I've been working on this for quite a while now and im stuck. Hopefully i can get some answers here so i can continue with it.


CREATE TRIGGER tr_orders ON orders
AFTER INSERT
AS
SELECT suppliers.sname AS supplier, parts.pname AS Part, orders.qty AS quantity
FROM orders
INNER JOIN parts ON parts.pno = orders.pno
INNER JOIN suppliers ON suppliers.sno = orders.sno
WHERE suppliers.city != parts.city
BEGIN
RAISERROR ('Order Rejected, supplier and part in the same city ', 16, 1)
ROLLBACK TRAN
RETURN
END
GO

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2010-02-22 : 22:30:56
Looks like you need to enclose your SELECT statement in a IF EXISTS() expression:

if exists (SELECT...)
begin
raiserror...
end


EDIT:
Also, you should INNER JOIN [inserted] so that you're only dealing with the row(s) inserted by the user for this transaction.

Be One with the Optimizer
TG
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-02-22 : 22:42:00
quote:
Originally posted by Sql_question

Hello, im new to sql and im struggeling with my trigger query.
There are 3 tables: -orders,
-parts and
-suppliers

I have to write an AFTER INSERT Trigger on the orders table to verify that the supplier and the part specified in an insert statement are in the same city. Then i have to reject all orders which do not have a supplier and part in the same city and print a message to the screen with the reason for rejecting the order. For valid orders, i have to print a message to the screen indicating that the order was valid.

I made this so far, but when i try to insert data in the orders table they all get rejected by the trigger. I've been working on this for quite a while now and im stuck. Hopefully i can get some answers here so i can continue with it.


CREATE TRIGGER tr_orders ON orders
AFTER INSERT
AS
SELECT suppliers.sname AS supplier, parts.pname AS Part, orders.qty AS quantity
FROM orders
INNER JOIN parts ON parts.pno = orders.pno
INNER JOIN suppliers ON suppliers.sno = orders.sno
WHERE suppliers.city != parts.city
BEGIN
RAISERROR ('Order Rejected, supplier and part in the same city ', 16, 1)
ROLLBACK TRAN
RETURN
END
GO

Let me modify your original query to the way TG suggested.

CREATE TRIGGER tr_orders ON orders
AFTER INSERT
AS
IF Exists(
SELECT 1 FROM Inserted I
INNER JOIN parts ON parts.pno = I.pno
INNER JOIN suppliers ON suppliers.sno = I.sno
WHERE suppliers.city <> parts.city)

BEGIN
RAISERROR ('Order Rejected, supplier and part in the same city ', 16, 1)
ROLLBACK TRAN
RETURN
END
GO





PBUH
Go to Top of Page

Sql_question
Starting Member

2 Posts

Posted - 2010-02-22 : 23:25:38
Thank you!

It's working and i think i understand what you wrote :)
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-02-23 : 00:47:48
quote:
Originally posted by Sql_question

Thank you!

It's working and i think i understand what you wrote :)



You are welcome

PBUH
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-23 : 09:26:22
i think message should read

RAISERROR ('Order Rejected, supplier and part should be in the same city ', 16, 1)


for better clarity

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -