Groups | Blog | Home
all groups > sql server programming > september 2003 >

sql server programming : delete self referencing foreign key


Pravin
9/2/2003 10:55:39 PM
I have a table named Emp as follows:

empid int
empname varchar(35)
mgrid int

empid empname mgrid
1 abc null
2 pqr 1
3 lmn 2
4 xyz null

empid is primary key. mgrid is self referencing foreign key field towards
empid.

Now if user says delete empid = 1 then i want to:
a. delete the record where empid = 1
b. delete all the records where mgrid = 1; in our case where empid = 2.
c. delete record where empid = 3 as it's mgrid is 2.

So this way it should go on doing cascade delete. Since the table has self
referencing foreign key this is not possible. please give me script (if
possible) that will help me. Thanks in advance.


Vishal Parkar
9/3/2003 1:13:16 AM
see my reply in .server group.

--
-Vishal
[quoted text, click to view]

AddThis Social Bookmark Button