Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm looking for a little help in creating triggers. Below is the first trigger that I have corrected. Please could you let me know if I have the right Idea or maybe some helpful hints.

SQL
Create Table CustomerLog (
	CustomerLogID int IDENTITY(1,1),
	CustomerID int NOT NULL,
	Email varchar(100),
	LogDate datetime
	)
GO

Create TRIGGER trgCustomerInsert 
ON Customer 
FOR INSERT AS

	DECLARE @TheCustomerID int
	DECLARE @Email varchar (100)
	Select @TheCustomerID = CustomerID From inserted
	Select @Email = Email From inserted
	INSERT INTO CustomerLog (CustomerLogID, CustomerID, Email, LogDate)
		Values('CustomerLogID',@TheCustomerID,@Email,GetDate())

GO



The next trigger I have to create is a INSERT UPDATE trigger where I'm updating the QTY on a different table. I believe I have the most of it correct however I'm not sure on how to write the update for the product table. What am I doing wrong in the trigger design? I'm really confused on how to get the correct output. Is there some information that would shine some light on my problem.

SQL
Create Table CustomerOrderItemLog (
	CustomerOrderItemID int IDENTITY (1,1),
	CustomerOrderID int,
	Qty int,
	UpdatedQty int
	)
GO

Create TRIGGER trgCustomerOrderItem
ON CustomerOrderItem,Product
FOR INSERT, UPDATE AS
	DECLARE @TheCustomerOrderID int
	DECLARE @TheQTY int
	Select @TheCustomerOrderID = CustomerID from inserted
	Select @TheQTY = QTY from inserted
	Select @OnHandQTY = QTY from Product
	INSERT INTO CustomerOrderItemLog (CustomerOrderItemID, CustomerOrderID,Qty,UpdatedQty)
		Values(
Posted

1 solution

About the first trigger, it looks just fine and id it does what is required, then it should be ok. If you like, you can simplify it a bit, for example:
SQL
Create TRIGGER trgCustomerInsert
ON Customer
FOR INSERT AS
    INSERT INTO CustomerLog (CustomerLogID, CustomerID, Email, LogDate)
    SELECT 'CustomerLogID', TheCustomerID, Email, GetDate()
    FROM inserted

About the second trigger, if the problem is how to update another table just like you insert into another table. Could you describe a bit more what's the desired behavior?

ADDITION:

Based on your comment, you shouldn't use the second trigger on Product table. If the CustomerOrderItem entry is subtracting the qauntity, then your trigger could be something like:
SQL
Create TRIGGER trgCustomerOrderItem
ON CustomerOrderItem
FOR INSERT, UPDATE AS
    UPDATE Product SET QTY = QTY - (SELECT SUM(QTY) FROM inserted)
    WHERE here define the condition for correct product based on CustomerOrderItem???

So you reduce the quantity of the product based on the inserted rows. I didn't see any product information on CustomerOrderItem table so I didnät know the joining condition for the update. Perhaps it's in the Order table.


The triggers are just like for example a stored procedure, a bunch of T-SQL statements that are run when the triggering action occurs. Few links you should go through:
- DML Triggers[^]
- CREATE TRIGGER[^]
- Multirow Considerations for DML Triggers[^]
 
Share this answer
 
v2
Comments
Anthony Bond 18-Dec-11 14:59pm    
What I'm trying to do is update the Qty in the product table. By taking the Qty ordered and subtracting that from Product.Qty, leaving me with the new Qty amount in the product table. I'm not really sure on how to write that into the trigger. Do you know where I can find some really good examples of Triggers and how they work?
Wendelius 18-Dec-11 15:27pm    
Answer updated
Anthony Bond 18-Dec-11 19:50pm    
Thank you very much for your time. This helped out tremendously.
Wendelius 18-Dec-11 23:40pm    
You're welcome :)

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