1
我需要獲取存儲過程(不是標準的,只有自定義)才能刪除未使用的行表(table1)和其他一些表(table2,table3,table4 ...- 我們不知道FOREIGN KEY CONSTRAINT)可以引用多少個表引用table1。SQL Server - 刪除未使用的行
例如:
С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
有在Table 1和表2,表3是指表1(提醒的是,我們不知道有多少表是指由外鍵約束表1)
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)
有在表行之前調用存儲過程
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
而且後 [DBO]。[delete_on_constraints]「table1的「身份證」過程調用
table1
id value
1 100
2 200
3 300