从多个 child table 中删除行后,从 parent table 中动态删除行
Dynamically delete rows from parent table after deleting rows from multiple child table
我有一个脚本,它将 return parent 和 child table 详细信息与各自的关键列和 table'应该删除 s 行。
以下是脚本的详细信息 return:
| Child_table| Child_column| Parent_Table| Parent_Column| Delete_Order|
|:-----------|:------------| ------------| -------------|-------------|
| Child1 | Child1_ID | Child2 | Child2_Id | 2 |
| Child2 | Child2_ID1 | Parent1 | Parent_ID | 1 |
| Child3 | Child3_ID | Parent1 | Parent_ID | 1 |
| Parent1 | Parent_ID | | | 0 |
在上面的table中,Child1
是[=16的childtable =]和Child2
和Child3
是childtable Parent1
的小号。现在我需要按照 Delete_Order
列中给出的降序从 child 到 parent table 删除数据。
我需要帮助来编写一个将连接这些 table 和
根据主键数据删除Child和Parenttable中的数据
来自 parent table.
使用递归 CTE 创建 DELETE 语句。语句必须 运行 do desc
顺序。将“=1”替换为适当的值。
with cte as (
select cast('delete' + x.childAlias + ' from ' + Child_table + x.childAlias +
case when Parent_Column is null then ' where ' + Child_column + ' = 1;' else '' end as nvarchar(max)) sql,
x.childAlias, Child_column,
Parent_Table, Parent_Column,
Delete_Order do
from tbl
cross apply (select ' t' + cast(tbl.Delete_Order as varchar(3))) x(childAlias)
union all
select cte.sql + ' join ' + tbl.Child_table + x.childAlias +
' on' + cte.childAlias+ '.' + cte.Child_column + ' =' + x.childAlias + '.'+ cte.Parent_Column +
case when tbl.Parent_Column is null then ' where' + x.childAlias + '.' + tbl.Child_column + ' = 1;' else '' end sql,
x.childAlias, tbl.Child_column,
tbl.Parent_Table, tbl.Parent_Column,
do
from cte
join tbl on cte.Parent_Table = tbl.Child_table
cross apply (select ' t' + cast(tbl.Delete_Order as varchar(3))) x(childAlias)
)
select sql, do
from cte
where sql like '%where%'
order by do desc
我有一个脚本,它将 return parent 和 child table 详细信息与各自的关键列和 table'应该删除 s 行。 以下是脚本的详细信息 return:
| Child_table| Child_column| Parent_Table| Parent_Column| Delete_Order|
|:-----------|:------------| ------------| -------------|-------------|
| Child1 | Child1_ID | Child2 | Child2_Id | 2 |
| Child2 | Child2_ID1 | Parent1 | Parent_ID | 1 |
| Child3 | Child3_ID | Parent1 | Parent_ID | 1 |
| Parent1 | Parent_ID | | | 0 |
在上面的table中,Child1
是[=16的childtable =]和Child2
和Child3
是childtable Parent1
的小号。现在我需要按照 Delete_Order
列中给出的降序从 child 到 parent table 删除数据。
我需要帮助来编写一个将连接这些 table 和 根据主键数据删除Child和Parenttable中的数据 来自 parent table.
使用递归 CTE 创建 DELETE 语句。语句必须 运行 do desc
顺序。将“=1”替换为适当的值。
with cte as (
select cast('delete' + x.childAlias + ' from ' + Child_table + x.childAlias +
case when Parent_Column is null then ' where ' + Child_column + ' = 1;' else '' end as nvarchar(max)) sql,
x.childAlias, Child_column,
Parent_Table, Parent_Column,
Delete_Order do
from tbl
cross apply (select ' t' + cast(tbl.Delete_Order as varchar(3))) x(childAlias)
union all
select cte.sql + ' join ' + tbl.Child_table + x.childAlias +
' on' + cte.childAlias+ '.' + cte.Child_column + ' =' + x.childAlias + '.'+ cte.Parent_Column +
case when tbl.Parent_Column is null then ' where' + x.childAlias + '.' + tbl.Child_column + ' = 1;' else '' end sql,
x.childAlias, tbl.Child_column,
tbl.Parent_Table, tbl.Parent_Column,
do
from cte
join tbl on cte.Parent_Table = tbl.Child_table
cross apply (select ' t' + cast(tbl.Delete_Order as varchar(3))) x(childAlias)
)
select sql, do
from cte
where sql like '%where%'
order by do desc