CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2003
    Location
    España
    Posts
    24

    delete trigger cuestion

    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.

  2. #2
    Join Date
    Jun 2001
    Location
    Sri Lanka
    Posts
    272

    Re: delete trigger cuestion

    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);

  3. #3
    Join Date
    Oct 2005
    Location
    India
    Posts
    24

    Red face Re: delete trigger cuestion

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured