Click to See Complete Forum and Search --> : delete trigger cuestion


El Puto Amo
October 14th, 2005, 12:25 PM
Hello

I have a DB in SQL-Server 2000 and I want to make a trigger to delete rows in a table when a row in a realtioned table is deleted.

I attempt to do this using foreign key restrittions but in my case it isn't possible because it will create cicles.

I need the same behaviour as a foreign key but using trigger.

Please help me.

srinika
October 14th, 2005, 01:00 PM
If I understand u correctly,

U have 2 tables T1 & t2
T1 has fields F1 & F2
T2 has fields F1 & F3
Where
T1.F1 is a primary key &
T2.F1 is a foreign key

and sample data of the 2 tables are as follows;
T1:
F1 F2
---------
a p
b q
c r
d p

T2 :
F1 F2
--------
a L
a M
b L
b M
d N

What u want to do is to write a trigger to T1, when a record is deleted the corresponding records of T2 to be deleted (by the trigger)
eg.
When a SQL as follows is executed:
Delete from T1 where F1 = 'b'

U want to delete the 2 records in T2 which has F1 = 'b'

The Trigger should have something like :

Delete from T2 where F1 = (Select F1 from DELETED);

trends
October 15th, 2005, 01:54 AM
HI ,

I have tested the resply provided by srinika with sample tables and i found it working superbly.So try that and you surely get the solution for that.The trigger may be like this.

Example:

create trigger abcd on table1
after delete
as
delete from table2 where cust1 =(select cust1 from deleted)

you have to give "after" in cursor.Please note that