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

sql server programming

group:

creating triggers (when columns updated or deleting too many rows)


creating triggers (when columns updated or deleting too many rows) ryan332211 NO[at]SPAM hotmail.com
9/25/2006 5:00:43 PM
sql server programming:
I have a database with 300K rows, each row has 80 columns. I would
like to:

1. update one of the columns "LastUpdateColumn" whenever one of 10
specific columns have changed. This column would have a timestamp
field. This will give me a representation of the last time one of the
10 critical fields has been updated.
2. I would also insert a row into another table (an audit table).

I would ignore if one of the the other 70 columns have changed value,
only care if one of the 10 specific columns have changed value.

I have never done a trigger before and have not been able to find a
sample that would show something like this.

Also, i am interested in building a trigger on the DELETE and UPDATE
statement that would roll back the transaction if the DELETE statement
deletes more than one row or if the UPDATE statement updates more than
an arbitrary number of rows (maybe 1, maybe 10, it is something that i
will hard code, but am not certain of the number right now).

I have found samples of triggers when a delete statement is executed,
but how do i (1) test the number of rows that are desired to be
deleted, and (2) if it is more than one, how do i roll back the entire
transaction gracefully so that the client gets a result "0 rows
deleted" ?

Any help is greatly appreciated. Thanks
Re: creating triggers (when columns updated or deleting too many rows) Arnie Rowland
9/25/2006 5:45:47 PM
To limit a trigger action to one row, add the following immediately =
after the AS:

IF @@ROWCOUNT =3D 0 -- Nothing to do, leave
RETURN

IF @@ROWCOUNT > 1 -- Too many rows, undo them all
BEGIN
ROLLBACK
RETURN
END

I would tend to separate UPDATE and DELETE actions into independent =
triggers.

For the issue of actions to take based upon columns updated, read up on =
UPDATED() or UPDATING() in Books Online.=20
You can 'direct traffic' by using:

IF UPDATE(ColumnName)
BEGIN
{actions to take, including inserts to another table}
END

UPDATING() does the same thing, but allows multiple columns (in the form =
of bitmaps) to be checked.

By the way, a TIMESTAMP datatype is not a datetime value, but is a =
sequential integer that is changed every time ANY column on the row is =
changed. I suspect you meant to indicate that you have a datetime field =
that needs to be changed to the last modification date/time. Using =
[TIMESTAMP] as a column name is confusing, and 'should' be avoided. Why =
not call it?'LastCriticalFieldUpdate'?


--=20
Arnie Rowland, Ph.D.
Westwood Consulting, Inc

Most good judgment comes from experience.=20
Most experience comes from bad judgment.=20
- Anonymous


[quoted text, click to view]
Re: creating triggers (when columns updated or deleting too many rows) Arnie Rowland
9/26/2006 4:24:15 PM
Ouch -I knew that!

(Your turn -Don't need to @@ on @rc.)

;-)

--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc

Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous


[quoted text, click to view]

Re: creating triggers (when columns updated or deleting too many rows) Hugo Kornelis
9/27/2006 12:30:29 AM
[quoted text, click to view]

Hi Arnie,

Not good - the first IF will reset @@ROWCOUNT before the second IF
starts.

DECLARE @rc int
SET @rc = @@ROWCOUNT

IF @@rc = 0 -- Nothing to do, leave
RETURN

IF @@rc > 1 -- Too many rows, undo them all
BEGIN
ROLLBACK
RETURN
END

[quoted text, click to view]

Agreed.

[quoted text, click to view]

Beware that this only tells if the column was present in the SET clause
of the UPDATE statement, not whether the new data is actuallly different
from the original data.

[quoted text, click to view]

I presume you meant COLUMNS_UPDATED(). Don't use it - it's ugly,
impossible to understand, even harder to maintain, plus it introduces an
undocumented and unnecessary dependency on column order.

--
Re: creating triggers (when columns updated or deleting too many rows) Hugo Kornelis
9/27/2006 11:24:21 PM
[quoted text, click to view]

Double ouch!!!

Would you buy it if I tried to blame my keyboard?

--
AddThis Social Bookmark Button