T-sql Update Trigger Query

Hi,
See below my update Query
 
Alter Trigger  [dbo].[empBack]
ON [DB].[dbo].[mainSal]

AFTER UPDATE
    AS
declare @emp_num nvarchar(7)
DECLARE @AnuualSalary decimal(18,2)


SELECT @emp_num =i.emp_num,@AnuualSalary=i.salary from inserted i;  
   
   UPDATE mainSal
   Set salary=0.00,annual=0.00
   where emp_num=@emp_num
   
      UPDATE empBack 
   Set AnnualSalary=@AnuualSalary
   where emp_num=@emp_num

GO

Open in new window


What i need is if someone insert record in "mainSal" Table Salary and anuual salary going to be 0 and
empBack table updated record is going to be save.

My getting following error

"Maximum stored procedure function,trigger or View Nesting Level exceeded limt 32"

Can some one show me where im doing the mistake. Thanks
LVL 10
ukerandiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dany BalianCTOCommented:
the sql:
UPDATE mainSal
   Set salary=0.00,annual=0.00
   where emp_num=@emp_num

Open in new window

will force the trigger to run again.

the solution would be to read the old values, to see if they are 0 before running the update.

SELECT @AnuualSalary=d.salary from deleted d;
if @anuualSalary>0 //this means that there were some values in the table before the update
begin
SELECT @emp_num =i.emp_num,@AnuualSalary=i.salary from inserted i; //fetching the new values
UPDATE mainSal
   Set salary=0.00,annual=0.00
   where emp_num=@emp_num;
UPDATE empBack 
   Set AnnualSalary=@AnuualSalary
   where emp_num=@emp_num;
end 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dale BurrellDirectorCommented:
OK, there are couple of flaws in your trigger.

1/ You should never, ever assume that you're only getting a single record update. SQL uses set based logic and so should you unless there is a very good reason not to, in which case you have to loop through all records.

So you want to design your update a bit like this, so it can handle any number of records:

   UPDATE mainSal set
     salary=0.00,annual=0.00
   from mainSal, inserted i
   where mainSal.emp_num = i.emp_num

Open in new window


2/ Your trigger is going to create an infinite loop, because you are updating the table that the trigger runs on. And triggers always fire even if no records are update! Thats a trap for new players, so when updating the same table as the trigger, you need to make your update statement conditional to ensure at some point it stops firing e.g.

if exists ({some condition for update}) being
  update ...
end

Open in new window


I don't quite understand what your update is trying to achieve so I can't be anymore specific than that unfortunately.

HTH
0
ukerandiAuthor Commented:
Excellent,thank you both of you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.