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
 Update the same table in the trigger

Author  Topic 

Fouad Kayali
Starting Member

1 Post

Posted - 2012-03-20 : 03:30:37
Hi,

I have a table which calculate the total deduction and the remaining deduction from the previous month.
and any update in the remaining deduction of old month should affect the next months in order, so I've created AFTER UPDATE Trigger for this table, but I've noticed that the Trigger is not ivoked by the UPDATE CLAUSE for the next month.

Check the trigger code

CREATE TRIGGER [dbo].[tgUpdateRemainingBalance]
ON [dbo].[GAT_MONTHLY_DEDUCTION]
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;

DECLARE @OldTotalDeduction INT
DECLARE @NewTotalDeduction INT
DECLARE @OldRemainingToNextMonth INT
DECLARE @NewRemainingToNextMonth INT

SELECT @OldRemainingToNextMonth = RemainingToNextMonth,
@OldTotalDeduction = TotalDeduction
FROM DELETED

SELECT @NewRemainingToNextMonth = RemainingToNextMonth,
@NewTotalDeduction = TotalDeduction
FROM INSERTED

DECLARE @EmpNo BIGINT
DECLARE @RegDate DATETIME

SELECT @EmpNo = PersonnelNo, @RegDate = RegDate FROM INSERTED

IF (@OldRemainingToNextMonth <> @NewRemainingToNextMonth) OR (@OldTotalDeduction <> @NewTotalDeduction)
BEGIN
UPDATE GAT_MONTHLY_DEDUCTION
SET TotalDeduction = TotalDeduction - @OldRemainingToNextMonth + @NewRemainingToNextMonth,
Visited = 1
WHERE RegDate = DATEADD(month, 1, @RegDate) AND PersonnelNo = @EmpNo
END
END


I am doing updateing for the same table which has this triggers but calling the trigger for the first time should call the trigger again for the next update but it is not happening.

It is urgent for me to know how to solve it.

Thanks

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-03-20 : 07:59:33
I have 2 or three comments about your code:

1. INSERTED and DELETED virtual tables can have more than one row. When you assign the value from those tables to variables, which of the many values will get assigned is not predictable. Even if your business logic will result in only one row being updated, relying on that is not a good idea.

2. Looks like what you are trying to do is use recursive triggers. Recursive triggers are turned off by default. You can check if it is turned off by running this query:
SELECT is_recursive_triggers_on FROM sys.databases WHERE NAME = 'thedatabasename'
If it is indeed turned off, and you want to turn it on, it would be advisable to do a lot of testing and evaluation to see what if any impact that would have on other triggers.

3. I did not completely follow the logic that you are trying to implement, but it seemed like you are trying to recursively update a column until it settles down to some value. If that indeed is the case, there may be simpler and better alternatives other than using recursive triggers.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-20 : 15:08:20
i would suggest handling recursive call using common table expression inside trigger once rather than calling trigger itself recursively

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

Go to Top of Page
   

- Advertisement -