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

sql server programming

group:

Another Delete Trigger Twist...



Another Delete Trigger Twist... Chris Marsh
11/10/2004 9:10:45 PM
sql server programming: Hi keep getting excellent help so I am trying again. Hopefully, we will have
all our trigger twists resolved soon. Below is the trigger we are using for
a variety of tables but I am now getting an error that I don't understand
exactly how to solve:

CREATE TRIGGER [SaveDeletedCustomer] ON [dbo].[customer]

INSTEAD OF DELETE -- Must use INSTEAD OF as the table has many Text
columns.

AS

insert into delcustomer select * from deleted del --Save deleted record in
the Delcustomer table


DELETE customer FROM deleted where customer.cusid = deleted.cusid


EXEC master..xp_sendmail 'Administrator',

'A Customer Record was just DELETED.'

The Error states:

"Cannot insert a non-null value into a timestamp column. Unse INSERT with a
column list or with a default of NULL for the timestamp column.

Why we even have a timestamp column I don't know (we have them on a few
tables but they don't appear to be updated). So, the big question is how do
I fix the delete error. My guess is that you are going to tell me that I
have to define each field with is a pain because we have probably 150 or
more columns.

Thanks,

Chris

Re: Another Delete Trigger Twist... Mike Epprecht (SQL MVP)
11/11/2004 2:22:03 AM
Hi

To add, If you send an -email from a trigger and the e-mail sending fails,
your transaction gets rolled back. Debugging such an issue is very diffucult
once in production.

Regards
Mike

[quoted text, click to view]
Re: Another Delete Trigger Twist... David Portas
11/11/2004 7:58:14 AM
Yes, you should ALWAYS list the column names in an INSERT statement. It's
the only safe way. In Query Analyzer you can drag the column name list from
the Object Browser so you don't need to do any extra typing.

Don't send email from a trigger. There's no need to hold open a transaction
while the mail is sent. INSERT a row into another table to record the event
and then send out email on a regular schedule as part of a SQL Agent job.

--
David Portas
SQL Server MVP
--

Re: Another Delete Trigger Twist... Hugo Kornelis
11/11/2004 10:27:06 AM
[quoted text, click to view]

Hi Chris,

David has already answered most of your questions, but didn't comment on
this remark.

A timestamp column is never explicitly updated. It is not even allowed. A
timestamp column (also called rowversion column - the name timestamp is
grossly misleading, as there is no way to extract a time from the value in
these columns) is automatically changed whenever a row is changed by an
UPDATE statement. It is also automatically filled when the row is
inserted.

The raison d'être for a timestamp column is optimistic locking: an
application reads a row without locking it, stores the timestamp value
internally, shows it on a screen and waits for the user to proceed. If the
user changes any values, an UPDATE statement is sent to the server; this
statement includes a WHERE timestamp_column = old_timestamp_value. If this
where cluase is not matched, no row is affected by the update and the user
is informed that his changes could not be saved because another user or
process has already changed the row.

Best, Hugo
--

Re: Another Delete Trigger Twist... Chris Marsh
11/11/2004 1:41:29 PM
Thank you! Another question and answer resolved. I appreciate the help.


[quoted text, click to view]

Re: Another Delete Trigger Twist... Chris Marsh
11/11/2004 1:41:40 PM
Thank you! Another question and answer resolved. I appreciate the help.

[quoted text, click to view]

Re: Another Delete Trigger Twist... Chris Marsh
11/11/2004 1:41:54 PM
Thank you! Another question and answer resolved. I appreciate the help.

[quoted text, click to view]

Re: Another Delete Trigger Twist... jshotwell
11/11/2004 2:07:44 PM
Yes, using the column list is the best approach (if not the only approach).
But you can have the system generate most of the code for you using
something like the following:

select ' , ' + name
from syscolumns
where id = object_id('delcustomer ')
order by colorder

- James

[quoted text, click to view]

Re: Another Delete Trigger Twist... Chris Marsh
11/11/2004 8:32:50 PM
Another great tip! Thank you!

[quoted text, click to view]

Re: Another Delete Trigger Twist... Chris Marsh
11/11/2004 10:30:53 PM
Okay, so now I am cooking on my delete triggers, have them setup on about a
dozen key tables and it's working like a champ. It would be really cool to
know who has deleted the record and log that in the Delxxxx table. Or, is
there another approach that is better.

Any example on how I can log a users delete including the date and time. It
would really be amazing if I even knew what PC they did it from?

Thanks again.

[quoted text, click to view]

AddThis Social Bookmark Button