I need to get stored procedure (not standart, only custom) to delete unused rows table (table1) and some other tables (table2, table3, table4…- we don’t know how many tables refers to table1 by FOREIGN KEY CONSTRAINT) can refers to this table.
For example:
СREATE PROCEDURE [dbo].[delete_on_constraints]
@table varchar(36) – table name we need to delete unused rows
@column varchar(36) – column name - primary key, on which other tables refers by FOREIGN KEY CONSTRAINT
There are the table1 and table2, table3 which refers to table1 (remind, we don’t know how many tables refers to table1 by FOREIGN KEY CONSTRAINT)
CREATE TABLE [table1] (
[id] [int] IDENTITY (1, 1) NOT NULL CONSTRAINT [PK_ table1] PRIMARY KEY,
[value] [int] NULL
)
GO
CREATE TABLE [table2] (
[id] [int] IDENTITY (1, 1) NOT NULL CONSTRAINT [PK_ table2] PRIMARY KEY,
[table1_id] [int] NOT NULL ,
CONSTRAINT [FK_ table2_ table1] FOREIGN KEY ([table1_id]) REFERENCES [table1] ( [id] )
)
GO
CREATE TABLE [table3] (
[id] [int] IDENTITY (1, 1) NOT NULL CONSTRAINT [PK_ table3] PRIMARY KEY,
[table1_id] [int] NOT NULL ,
CONSTRAINT [FK_ table3_ table1] FOREIGN KEY ([table1_id]) REFERENCES [table1] ( [id] )
)
GO
INSERT INTO [table1] VALUES (100)
INSERT INTO [table1] VALUES (200)
INSERT INTO [table1] VALUES (300)
INSERT INTO [table1] VALUES (400)
GO
INSERT INTO [table2] VALUES (1)
INSERT INTO [table2] VALUES (3)
GO
INSERT INTO [table3] VALUES (1)
INSERT INTO [table3] VALUES (2)
INSERT INTO [table3] VALUES (3)
There are rows in tables before call stored procedure
table1
id value
1 100
2 200
3 300
4 400
table2
id table1_id
1 1
2 3
table3
id table1_id
1 1
2 2
3 3
And after [dbo].[delete_on_constraints] ‘table1’ ‘id’ procedure call
table1
id value
1 100
2 200
3 300
First, let me state for the record that this code should never be used in production. By that I mean, it should only ever be used as a one-time cleanup. If you have regular need of deleting rows without knowing the DRI, then there is a fundamental design problem.
In short, you need to use dynamic SQL to query for the relations and build the Delete statements.
Btw, let me also mention that a better solution might be to enable Cascade Delete on your relations. With cascade delete, the need for this routine would be moot.
See Cascading Referential Integrity Constraints for more.