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.
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 -suppliersI 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 ASSELECT suppliers.sname AS supplier, parts.pname AS Part, orders.qty AS quantityFROM ordersINNER JOIN parts ON parts.pno = orders.pnoINNER JOIN suppliers ON suppliers.sno = orders.snoWHERE suppliers.city != parts.city BEGIN RAISERROR ('Order Rejected, supplier and part in the same city ', 16, 1) ROLLBACK TRAN RETURN ENDGO |
|
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 OptimizerTG |
|
|
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 -suppliersI 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 ASSELECT suppliers.sname AS supplier, parts.pname AS Part, orders.qty AS quantityFROM ordersINNER JOIN parts ON parts.pno = orders.pnoINNER JOIN suppliers ON suppliers.sno = orders.snoWHERE suppliers.city != parts.city BEGIN RAISERROR ('Order Rejected, supplier and part in the same city ', 16, 1) ROLLBACK TRAN RETURN ENDGOLet me modify your original query to the way TG suggested.CREATE TRIGGER tr_orders ON orders AFTER INSERT ASIF Exists(SELECT 1 FROM Inserted IINNER JOIN parts ON parts.pno = I.pnoINNER JOIN suppliers ON suppliers.sno = I.snoWHERE suppliers.city <> parts.city) BEGIN RAISERROR ('Order Rejected, supplier and part in the same city ', 16, 1) ROLLBACK TRAN RETURN ENDGO
PBUH |
|
|
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 :) |
|
|
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 welcomePBUH |
|
|
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 MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|
|
|