Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 删除所有相关记录_Sql_Sql Server 2008_Sql Server 2012 - Fatal编程技术网

Sql 删除所有相关记录

Sql 删除所有相关记录,sql,sql-server-2008,sql-server-2012,Sql,Sql Server 2008,Sql Server 2012,我有几张像下面这样的桌子 每个表都有一个主键\u id。不幸的是,即使一个表引用另一个表的主键(例如:table2,table2\u 1正在引用table1.table1\u id),在其他表上也没有外键设置,现在不允许我更改此设计 当根据主键从表中删除记录时,我需要删除所有关联的记录 说明: 如果我在table1中删除table1\u id=10的记录,我还应该删除table1\u id是表结构一部分的其他表中的记录(table2和table2\u 1)。当在table2中删除记录时,我还应

我有几张像下面这样的桌子

每个表都有一个主键
\u id
。不幸的是,即使一个表引用另一个表的主键(例如:
table2
table2\u 1
正在引用
table1.table1\u id
),在其他表上也没有外键设置,现在不允许我更改此设计

当根据主键从表中删除记录时,我需要删除所有关联的记录

说明:

如果我在
table1
中删除
table1\u id=10
的记录,我还应该删除
table1\u id
是表结构一部分的其他表中的记录(
table2
table2\u 1
)。当在
table2
中删除记录时,我还应该删除
table2\u id
是表结构一部分的其他表中的记录(
table3
),我需要遍历,直到没有依赖项,并删除所有相关记录

我尝试生成如下查询(假设我想删除table1_=10的记录):

创建表table1(表1_id INT IDENTITY(10,1)、表1_field1 varchar(100)、表1_field2 BIT);
创建表table2(表2_id INT IDENTITY(100,1)、表2_field1 varchar(100)、表2_field2 BIT、表1_id INT);
创建表table2_1(table2_1_id INT IDENTITY(500,1),table2_1_field1 varchar(100),table2_1_field2 BIT,table1_id INT);
创建表table3(表3\u id INT IDENTITY(1000,1)、表3\u field1 varchar(100)、表3\u field2 BIT、表2\u id INT);
创建表table4(表4_id INT IDENTITY(10000,1)、表4_field1 varchar(100)、表4_field2 BIT、表3_id INT);
创建表table5(表5_id INT IDENTITY(100000,1),表5_field1 varchar(100),表5_field2 BIT,表4_id INT);
在表1(表1_字段1,表1_字段2)中插入值('table1_字段1_1',0),('table1_字段1_2',1),('table1_字段1_3',0),('table1_字段1_4',1);
将值('table2_field1_1',0,10),('table2_field1_2',1,11),('table2_field1_3',0,12),('table2_field1_4',1,13)插入表2(table2_field1,0,12);
在表2_1(表2_1_字段1,表2_1_字段2,表1_id)中插入值('表2_1_字段1_1',0,10),('表2_1_字段1_2',1,11),('表2_1_字段1_3',0,12),('表2_1_字段1_4',1,13);
在表3(表3_字段1、表3_字段2、表2_id)中插入值('table3_字段1_1',0,100),('table3_字段1_2',1,101),('table3_字段1_3',0,102),('table3_字段1_4',1,103);
在表4(表4_字段1、表4_字段2、表3_id)中插入值('table4_字段1_1',0,1000),('table4_字段1_2',1,1001),('table4_字段1_3',0,1002),('table4_字段1_4',1,1003);
在表5(表5_字段1、表5_字段2、表4_id)中插入值('table5_字段1_1',0,10000),('table5_字段1_2',1,10001),('table5_字段1_3',0,10002),('table5_字段1_4',1,10003);
从表1中选择*
从表2中选择*
从表2\u 1中选择*
从表3中选择*
从表4中选择*
从表5中选择*
选择t.name,c.name,'删除['+t.name+'],其中['+c.name+']=10',
'从sys.tables t internal join sys.columns c on t.object\u id=c.object\u id中选择t.name[tn],c.name[cn],其中c.name=''+t.name+'\u id''和t.name'+t.name+'''
从sys.t表
t.object\u id=c.object\u id上的内部联接sys.c列
其中c.name='table1_id'和t.name'table1'
按t.名称订购
/*使用SELECT查询查找其他依赖项,如下所示*/
选择t.name[t]、c.name[c]、从sys.tables t中选择t.name[tn]、c.name[cn]在t.object\u id=c.object\u id上内部连接sys.columns c,其中c.name='''+t.name+'\u id''和t.name'+t.name+''
从sys.tables t内部连接t.object\u id=c.object\u id上的sys.columns c,其中c.name='table2\u id'和t.name'table2'
选择t.name[tn]、c.name[cn]、从sys.tables t内部连接sys.columns c on t.object\u id=c.object\u id,其中c.name='''+t.name+'\u id''和t.name'+t.name+''
从sys.tables t内部连接t.object\u id=c.object\u id上的sys.columns c,其中c.name='table3\u id'和t.name'table3'
选择t.name[tn]、c.name[cn]、从sys.tables t内部连接sys.columns c on t.object\u id=c.object\u id,其中c.name='''+t.name+'\u id''和t.name'+t.name+''
从sys.tables t内部连接t.object\u id=c.object\u id上的sys.columns c,其中c.name='table4\u id'和t.name'table4'
从sys.tables t internal join sys.columns c on t.object\u id=c.object\u id中选择t.name[tn],c.name[cn],其中c.name='table5\u id'和t.name'table5'
/*我需要运行SQL查询,如下所示*/
删除表5中的表4\u id(从表4中的表3\u id中选择表4\u id(从表3中的表2\u id中选择表3\u id(从表2中的表1\u id=10选择表2\u id)))
删除表4中的表3\u id(从表3中选择表3\u id,从表2中选择表2\u id,其中表1\u id=10))
删除table2\u id所在的table3(从table1\u id=10的table2中选择table2\u id)
删除表2,其中表1\u id=10
删除表2\u 1,其中表1\u id=10
删除表1,其中表1\u id=10
我预计删除后会出现以下结果:

注意:

  • 请注意,我知道外键关系在这种情况下是如何有用的,但现在不允许我更改表结构/设计(即无法添加外键)

  • 我也不能添加触发器

  • 我希望使用T-SQL/DynamicSQL实现所需的结果

  • 在我的插图中,我只使用了5个表,但它可以是n个类似结构的表


  • 有没有人可以建议其他方法(或自动执行此操作),所以我只需传递table1.table1\u id,然后它就应该删除所有相关记录,请?

    解决此问题的标准方法是
    CREATE TABLE table1 (table1_id INT IDENTITY(10, 1), table1_field1 varchar(100), table1_field2 BIT);
    CREATE TABLE table2 (table2_id INT IDENTITY(100, 1), table2_field1 varchar(100), table2_field2 BIT, table1_id INT);
    CREATE TABLE table2_1 (table2_1_id INT IDENTITY(500, 1), table2_1_field1 varchar(100), table2_1_field2 BIT, table1_id INT);
    CREATE TABLE table3 (table3_id INT IDENTITY(1000, 1), table3_field1 varchar(100), table3_field2 BIT, table2_id INT);
    CREATE TABLE table4 (table4_id INT IDENTITY(10000, 1), table4_field1 varchar(100), table4_field2 BIT, table3_id INT);
    CREATE TABLE table5 (table5_id INT IDENTITY(100000, 1), table5_field1 varchar(100), table5_field2 BIT, table4_id INT);
    
    INSERT INTO table1(table1_field1, table1_field2) VALUES ('table1_field1_1', 0), ('table1_field1_2', 1), ('table1_field1_3', 0), ('table1_field1_4', 1);
    INSERT INTO table2(table2_field1, table2_field2, table1_id) VALUES ('table2_field1_1', 0, 10), ('table2_field1_2', 1, 11), ('table2_field1_3', 0, 12), ('table2_field1_4', 1, 13);
    INSERT INTO table2_1(table2_1_field1, table2_1_field2, table1_id) VALUES ('table2_1_field1_1', 0, 10), ('table2_1_field1_2', 1, 11), ('table2_1_field1_3', 0, 12), ('table2_1_field1_4', 1, 13);
    INSERT INTO table3(table3_field1, table3_field2, table2_id) VALUES ('table3_field1_1', 0, 100), ('table3_field1_2', 1, 101), ('table3_field1_3', 0, 102), ('table3_field1_4', 1, 103);
    INSERT INTO table4(table4_field1, table4_field2, table3_id) VALUES ('table4_field1_1', 0, 1000), ('table4_field1_2', 1, 1001), ('table4_field1_3', 0, 1002), ('table4_field1_4', 1, 1003);
    INSERT INTO table5(table5_field1, table5_field2, table4_id) VALUES ('table5_field1_1', 0, 10000), ('table5_field1_2', 1, 10001), ('table5_field1_3', 0, 10002), ('table5_field1_4', 1, 10003);
    SELECT * FROM table1
    SELECT * FROM table2
    SELECT * FROM table2_1
    SELECT * FROM table3
    SELECT * FROM table4
    SELECT * FROM table5
    
        select t.name, c.name, 'DELETE [' + t.name + '] WHERE [' + c.name + '] = 10',
        'SELECT t.name [tn], c.name [cn] FROM sys.tables t inner join sys.columns c on t.object_id=c.object_id where c.name=''' + t.name + '_id'' and t.name<>''' + t.name + ''' ' 
        from sys.tables t
        inner join sys.columns c on t.object_id=c.object_id
        where c.name='table1_id' and t.name<>'table1'
        order by t.name
    
        /* Use the SELECT queries to find other dependencies like below */
    
    SELECT t.name [t], c.name [c],'SELECT t.name [tn], c.name [cn] FROM sys.tables t inner join sys.columns c on t.object_id=c.object_id where c.name=''' + t.name + '_id'' and t.name<>''' + t.name + '''' 
    FROM sys.tables t inner join sys.columns c on t.object_id=c.object_id where c.name='table2_id' and t.name<>'table2'
    
    SELECT t.name [tn], c.name [cn], 'SELECT t.name [tn], c.name [cn] FROM sys.tables t inner join sys.columns c on t.object_id=c.object_id where c.name=''' + t.name + '_id'' and t.name<>''' + t.name + '''' 
    FROM sys.tables t inner join sys.columns c on t.object_id=c.object_id where c.name='table3_id' and t.name<>'table3'
    
    SELECT t.name [tn], c.name [cn], 'SELECT t.name [tn], c.name [cn] FROM sys.tables t inner join sys.columns c on t.object_id=c.object_id where c.name=''' + t.name + '_id'' and t.name<>''' + t.name + '''' 
    FROM sys.tables t inner join sys.columns c on t.object_id=c.object_id where c.name='table4_id' and t.name<>'table4'
    
    SELECT t.name [tn], c.name [cn] FROM sys.tables t inner join sys.columns c on t.object_id=c.object_id where c.name='table5_id' and t.name<>'table5'
    
    /* I need to run the SQL queries something like below */
    DELETE table5 where table4_id in (select table4_id from table4 where table3_id in (select table3_id from table3 where table2_id in (select table2_id from table2 where table1_id=10)))
    DELETE table4 where table3_id in (select table3_id from table3 where table2_id in (select table2_id from table2 where table1_id=10))
    DELETE table3 where table2_id in (select table2_id from table2 where table1_id=10)
    DELETE table2 where table1_id=10
    DELETE table2_1 where table1_id=10
    DELETE table1 where table1_id=10
    
    delete from table1 where table1_id = ####;
    delete from table2 where table1_id not in (select table1_id from table1);
    delete from table2_1 where table1_id not in (select table1_id from table1);
    delete from table3 where table2_id not in (select table2_id from table2);
    delete from table4 where table3_id not in (select table3_id from table3);
    delete from table5 where table4_id not in (select table4_id from table4);
    
    CREATE PROC delrec @id int AS
    BEGIN
     DELETE table5 where table4_id in 
      (select table4_id from table4 where table3_id in 
        (select table3_id from table3 where table2_id in 
          (select table2_id from table2 where table1_id=@id)))
     DELETE table4 where table3_id in 
      (select table3_id from table3 where table2_id in 
        (select table2_id from table2 where table1_id=@id))
     DELETE table3 where table2_id in 
      (select table2_id from table2 where table1_id=@id)
     DELETE table2 where table1_id=@id
     DELETE table2_1 where table1_id=@id
     DELETE table1 where table1_id=@id
    END