Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting from a self-referencing table ??

Status
Not open for further replies.

Rincewind

Programmer
Sep 3, 2001
1
GB
Hi Everyone,

I have inherited maintenance of a SQL Server 2000 database and schema.

I am trying to write some admin procedures to clean up unwanted data and I have a problem with the above.

We have a Hardware table with 2 self-referencing links. A Definition fragment follows:

Hardware table:
Id bigint (Primary Key) Not Null
ComponentOf bigint (Foreign Key to Id) Null
PrevId bigint (Foreign Key to Id) Null

Basically the ComponentOf column can be null or contain the Id of a Hardware record that it is a component (child) of. The PrevId column can be null or contain the id of the previous version of the record.

This last is a little weird: when we change a record an Instead Of trigger intercepts the modification and copies the entire record to a new record with a new Id (generated by an Identity property). The trigger then sets the PrevId of the current record to be the Id of the just-inserted record, i.e. the initial hardware record remains the current record and history is always added as subsequent inserts (seem the wrong way round, but I inherited it and it works so I'm leaving it alone).

The problem is: how can I delete a hardware record if it is already referenced by component and history records ? For referential integrity I would like to delete the components and history at the same time.

I have tried instead of triggers with all sorts of logic, but I just can't get it to work.

Anyone figured this out before ???

TIA

Mark ;-)))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top