Click here to Skip to main content
15,885,998 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
tblBudget(butID,butName,butAmount,IncurredTD_Amount)
tblInvoice(ID,ItemName,InvoiceAmount,butID)
here are my two tables, when i update the invoice table i would have a trigger kick in on any insert or update on invoice, that will reduced the budget amount intblbutget and also increase the INcurreTDAmount auomatically using the butID as reference in the tblInvoice(butAmount-InvoiceAmount,and IncurredTDAmount+INvoiceAmount)......is this possible? what are the performance issues if at all any? how can it be done.....thanks a million in advance

What I have tried:

AFTER INSERT,  UPDATE
AS
BEGIN
       SET NOCOUNT ON;
 
       DECLARE @Inv_Amount float,@Bud_ID int
 
       SELECT @Bud_ID = INSERTED.Bud_ID       
       FROM INSERTED
 
       INSERT INTO dbo.tblBudget(Bud_Amount,IncurTD_Amount,ID) IncurTD_Amount
       VALUES(Bud_Amount - @Inv_Amount, IncurTD_Amount + @Inv_Amount,'Inserted')
END
but i get an error:
Posted
Updated 28-Jun-17 1:34am
Comments
[no name] 28-Jun-17 6:26am    
Seems there was an issue to post the error message, I only can see "but i get an error: ". Makes sense to add the error message ;)
Richard Deeming 29-Jun-17 17:57pm    
Inserts, updates and deletes can affect more than one row. When they do, the trigger is fired once per statement, and the inserted and deleted virtual tables will contain multiple rows. Your trigger should be using set-based processing to handle this.

Also, your INSERT statement is not valid. I'm not sure whether that's a typo in your question?

SQL
CREATE TRIGGER TR_Budget ON [dbo].[tblInvoice] 
	FOR INSERT, UPDATE
	AS
	BEGIN
          -- your code here, update tblBudget
	END
 
Share this answer
 
v2
its complaninig about the coloumn name that dont match in the insert statement:
Msg 207, Level 16, State 1, Line 1

which is not possible as the i am referencing the right table but i guess i am not too well versed with SQL. Maybe a temporary table will do? dont know.....
 
Share this answer
 
Comments
RickZeeland 28-Jun-17 8:39am    
Please use the comment button to add comments.
Do you see more error information ? could be some constraint that a field must be unique (often used on ID field).

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