all groups > sql server programming > september 2004 >
You're in the

sql server programming

group:

simple trigger question...


simple trigger question... Michael Schwab
9/23/2004 11:53:40 PM
sql server programming: ....but too difficult for me (obviously).
Take the following test trigger:

Create Trigger "xxx_Trigger1"
On dbo.xxx
For update
As
declare @id int

SELECT @id=x.ID FROM xxx x inner join inserted i on i.id = x.id Inner join
deleted d on i.id = d.id

if update(x2)
begin
UPDATE xxx set X3=x2 WHERE ID=@id
end

--why is the trigger not reaching this? (it only responds to an update on
x2)
if update(x1)
BEGIN
UPDATE xxx set X2=x1 WHERE ID=@id
END


The idea is the following: when x1 is updated it should update x2 which
should update x3. When x2 is updated only x3 should be updated. What's wrong
with my brains?

Thanks,

Michael

Re: simple trigger question... David Portas
9/24/2004 12:36:07 AM
[quoted text, click to view]

Don't assume that only one row will be updated. Your trigger is faulty
because it will only ever update ONE row even for a multiple row update.

Try:

IF UPDATE(x2)
UPDATE xxx
SET x3=x2
WHERE EXISTS
(SELECT *
FROM Inserted
WHERE id = xxx.id
UNION ALL
SELECT *
FROM Deleted
WHERE id = xxx.id)
.... etc

However, it's difficult to conceive of many situations where updates like
this between normalized columns in a table would make sense. Maybe what you
really need is a redesign. Of course I could be completely wrong since I
don't know your data but your requirement does look a little odd - maybe you
could explain just what is represented by X1,X2,X3.

--
David Portas
SQL Server MVP
--

Re: simple trigger question... Michael Schwab
9/24/2004 9:16:58 AM
Good point. I was just playing with this script to try and find the error.
The real trigger is on a custom system table that strores settings that can
be altered at runtime. I am storing an int in x1 and a string in x2 for
single and multiple choice settings. Some settings, however, change the
behaviour of others and I think it makes more sense to store this than to do
it on the fly (cause it's quite a bit of data, for my application, anyway).

Jonathan's tip actually done the trick! Thank anyway, Michael

[quoted text, click to view]

Re: simple trigger question... Michael Schwab
9/24/2004 9:21:00 AM
Great, that did it! Quick question to the 32 max triggers during nesting:
how do you trap the error/wher does it occur? And: When is the data
committed, only at the end of all this can every trigger rely on its
predissesor to have changed the data, fullstop?

Thanks a lot for your help!

michael

[quoted text, click to view]

Re: simple trigger question... Jonathan Chong
9/24/2004 11:58:58 AM
Well, David Portas gave a good practice in trigger scripting.
Your problem might due to the server configuration. Take a look in BOL about
"Using Nested Triggers" and "RECURSIVE_TRIGGERS". May be you need to turn
recursive triggers on as:

ALTER DATABASE database_name
SET RECURSIVE_TRIGGERS ON
GO


[quoted text, click to view]

Re: simple trigger question... Jonathan Chong
9/24/2004 12:06:22 PM
Forgot to add this:
When you turn that to ON. You might end up error saying your maximum 32 of
nesting has reached.

[quoted text, click to view]

AddThis Social Bookmark Button