/ Published in: SQL
delete all the dependent rows in all the other tables (like cascade option)
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
Normally, IF you try AND DELETE a record FROM a TABLE that IS constrained BY a FOREIGN KEY, you’ll GET an error message. This PROCEDURE checks FOR any FOREIGN KEYS FOR the TABLE, deletes any child records, THEN deletes the intended record. It REFERENCES the system TABLES sysforeignkeys, sysobjects AND syscolumns. Sysforeignkeys does what it says ON the tin – it’s a list OF ALL FOREIGN KEYS IN the DATABASE. It doesn’t contain actual TABLE AND FIELD names, instead it contains links TO the sysobjects (TABLES, stored procedures, views etc) AND syscolumns (FIELDS). The PROCEDURE works LIKE this – IF we want TO DELETE a record FROM TABLE X, we look IN the sysforeignkeys TABLE FOR ALL REFERENCES WHERE TABLE X IS the parent TABLE. It may be involved IN several such FK’s. ALL we do IS recursively GO through these FK’s, deleting the child TABLE records that are linked TO the record we want TO DELETE. DELETE ALL records FROM TABLE X WHERE field1 equals '234' DELETE FROM X WHERE field1 = '234'- TABLE Y IS linked TO X through the Y_ID FIELD, so DELETE FROM Y WHERE Y_ID IN (SELECT Y_ID FROM X WHERE field1 = '234')- TABLE Z IS linked TO Y through the Z_ref FIELD DELETE FROM Z WHERE Z-REF IN (SELECT Z_ref FROM Y WHERE Y_ID IN (SELECT Y_ID FROM X WHERE field1 = '234')) AS you can see FROM the above example, IF one OF the child TABLES IS also involved IN a FK CONSTRAINT, we also need TO DELETE the relating ROWS IN it’s child TABLES. Here’s the code FOR the PROCEDURE: CREATE PROCEDURE spDeleteRows /* Recursive row delete procedure. It deletes all rows in the table specified that conform to the criteria selected, while also deleting any child/grandchild records and so on. This is designed to do the same sort of thing as Access's cascade delete function. It first reads the sysforeignkeys table to find any child tables, then deletes the soon-to-be orphan records from them using recursive calls to this procedure. Once all child records are gone, the rows are deleted from the selected table. It is designed at this time to be run at the command line. It could also be used in code, but the printed output will not be available. */ ( @cTableName VARCHAR(50), /* name of the table where rows are to be deleted */ @cCriteria nvarchar(1000), /* criteria used to delete the rows required */ @iRowsAffected INT OUTPUT /* number of records affected by the delete */ ) AS SET nocount ON DECLARE @cTab VARCHAR(255), /* name of the child table */ @cCol VARCHAR(255), /* name of the linking field on the child table */ @cRefTab VARCHAR(255), /* name of the parent table */ @cRefCol VARCHAR(255), /* name of the linking field in the parent table */ @cFKName VARCHAR(255), /* name of the foreign key */ @cSQL nvarchar(1000), /* query string passed to the sp_ExecuteSQL procedure */ @cChildCriteria nvarchar(1000), /* criteria to be used to delete records from the child table */ @iChildRows INT /* number of rows deleted from the child table */ /* declare the cursor containing the foreign key constraint information */ DECLARE cFKey CURSOR LOCAL FOR SELECT SO1.name AS Tab, SC1.name AS Col, SO2.name AS RefTab, SC2.name AS RefCol, FO.name AS FKName FROM dbo.sysforeignkeys FK INNER JOIN dbo.syscolumns SC1 ON FK.fkeyid = SC1.id AND FK.fkey = SC1.colid INNER JOIN dbo.syscolumns SC2 ON FK.rkeyid = SC2.id AND FK.rkey = SC2.colid INNER JOIN dbo.sysobjects SO1 ON FK.fkeyid = SO1.id INNER JOIN dbo.sysobjects SO2 ON FK.rkeyid = SO2.id INNER JOIN dbo.sysobjects FO ON FK.constid = FO.id WHERE SO2.Name = @cTableName OPEN cFKey FETCH NEXT FROM cFKey INTO @cTab, @cCol, @cRefTab, @cRefCol, @cFKName WHILE @@FETCH_STATUS = 0 BEGIN /* build the criteria to delete rows from the child table. As it uses the criteria passed to this procedure, it gets progressively larger with recursive calls */ SET @cChildCriteria = @cCol + ' in (SELECT [' + @cRefCol + '] FROM [' + @cRefTab +'] WHERE ' + @cCriteria + ')' print 'Deleting records from table ' + @cTab /* call this procedure to delete the child rows */ EXEC spDeleteRows @cTab, @cChildCriteria, @iChildRows OUTPUT FETCH NEXT FROM cFKey INTO @cTab, @cCol, @cRefTab, @cRefCol, @cFKName END Close cFKey DeAllocate cFKey /* finally delete the rows from this table and display the rows affected */ SET @cSQL = 'DELETE FROM [' + @cTableName + '] WHERE ' + @cCriteria print @cSQL EXEC sp_ExecuteSQL @cSQL print 'Deleted ' + CONVERT(VARCHAR, @@ROWCOUNT) + ' records from table ' + @cTableName