合并语句删除 [英] Merge statement delete

查看:115
本文介绍了合并语句删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表Table_Target(Id int)

I’ve two tables Table_Target(Id int)

Table_Child(childid int,Parent_Id int) - >这里Parent_Id引用Table_Target的Id。

Table_Child(childid int, Parent_Id int) -> here Parent_Id references Id of Table_Target.

使用(holdlock)作为目标合并到table_target中

Merge into table_target with (holdlock) as target

使用table_sourw作为源

Using table_sourw as source

在source.id = target.id

On source.id = target.id

匹配时

更新要更新的语句目标列

Update statement to update target columns

当目标不匹配时

插入要插入源表的语句

如果未与来源匹配则

               
删除

我知道删除会删除Table_Target中的行,但Table_Child中有一行引用了正在删除的ID。我试图从Table_Child写删除子行,但语法不支持它。有没有办法在Merge语句中删除父行之前处理删除子行

I know delete would delete the row from Table_Target but there is a row in Table_Child referencing the Id that is being deleted. I tried to write delete child row from Table_Child but syntax doesn’t support it. Is there a way to handle deleting child rows before deleting parent row in Merge statement?

TIA




推荐答案

首先,根据以下查询从Table_Child中删除行:

First, Delete the rows from Table_Child based on the following query:

DELETE FROM Table_Child WHERE Parent_Id IN (

DELETE FROM Table_Child WHERE Parent_Id IN (

   SELECT Id FROM Table_Target WHERE Id Not IN(SELECT Id FROM Table_Source)

   SELECT Id FROM Table_Target WHERE Id NOT IN (SELECT Id FROM Table_Source)

);

然后执行Merge语句。

And then execute Merge statement.


这篇关于合并语句删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆