views:

145

answers:

3

I want to have an instead of delete trigger so that I can get text field values out of the table row that's being deleted to preserve those fields when the actual delete occurs. For some reason I can't pull them from the Deleted table in a standard Delete trigger (SQL errors out).

Is there a way to do an actual delete within in an "instead of delete" trigger without making the trigger refire?

Or a better way of getting text fields once a row is deleted to save into a new record?

A: 

I once needed to catch records which were being deleted; I wrote a trigger which would copy records into a different table with the same structure as the first one: it went something like this:

create trigger [delta_del] on [customer]
for delete
as

declare <variables> from deleted;
open mycurs
fetch next from mycurs into @v1, @v2
while (@@fetch_status = 0
begin
insert into delta (fields) values (@v1, @v2)
fetch next from mycursor into @v1, @v2
end

close mycursor
deallocate mycursor

etc.
brismith
-1 for CURSOR in a trigger, not seeing "text" datatype
gbn
@brismith, cursor only as a last resort! you can replace your cursor with: `insert into delta (...fields...) SELECT ...fields... from deleted` It inserts one row for every row returned from the select. However, this will have a problem with the deprecated TEXT data type that the OP has mentioned.
KM
Thanks for the tip! I thought I had to iterate through the batch record-by-record, copying the record to the new table
brismith
A: 

It is probably better to do this with an After Trigger. Less complex.

If you have the following tables

CREATE TABLE [dbo].[Data](
[DocumentID] [smallint] NULL,
[Data] [nvarchar](max) NULL
) 

and

CREATE TABLE [dbo].[Audit](
[DocumentID] [smallint] NULL,
[Data] [nvarchar](max) NULL
) 

The following After trigger should insert a row into the audit table whenever a row is deleted from the Data Table

Create Trigger audit_Table_Deletes on dbo.Data for delete 
as
if @@rowcount = 0 return;
Insert into audit (DocumentID, Data) Select DocumentID, Data from deleted;
Go
TooFat
The only problem is that text fields can't be inserted from the deleted table.
Caveatrob
"Cannot use text, ntext, or image columns in the 'inserted' and 'deleted' tables."
Caveatrob
Can you convert the Text data to nvarchar(max)? It looks like the text ntext and image Data Types are deprecated according to this http://technet.microsoft.com/en-us/library/bb510662.aspx
TooFat
+3  A: 

This method should do the trick. An instead of trigger will do whatever you sepcify instead of doing the delete automatically. This means it is key that you do the delete in it manually or the record will not get deleted. It will not run recursively. It can only be done on a table without cascade delete enabled. Basically the trick is you join to the orginal table on the id field in order to get the data from the field you don't have access to in the deleted pseudotable.

create table dbo.mytesting (test_id int, sometext text)
go
create table dbo.myaudit (test_id int, sometext text)
go
insert into dbo.mytesting
values (1, 'test')
go

Create Trigger audit_Table_Deletes on dbo.mytesting INSTEAD OF delete  
as 
if @@rowcount = 0 return; 
Insert into dbo.myaudit (test_id, sometext) 
Select d.test_id, t.sometext from deleted d 
join dbo.mytesting t on t.test_id = d.test_id

Delete dbo.mytesting where test_id in (select test_id from deleted)
go

delete dbo.mytesting where test_id = 1
select * from dbo.mytesting
select * from dbo.myaudit 
Go 

drop table dbo.mytesting
drop table dbo.myaudit

If you can change the field to varchar(max) or nvarchar(max) that is the best solution though. Text and ntext are deprecated and should be removed altogether from SQL Server in the next version.

HLGEM