使用左连接和表变量进行删除

发布于 2024-07-15 21:55:28 字数 367 浏览 6 评论 0原文

也许我缺少括号或其他东西,但我很难从表变量中删除行,我在其中加入并寻找要加入的键。 如果它有价值,那么我就会摆脱它。 问题是我无法解析查询。 有任何想法吗?

declare @MrTemp 
(
    key1 int
   ,key2 int
)

insert into @MrTemp
select key1, key2 from ASourceTable

delete  
from @MrTemp mt
left join ARealTable art on mt.key1 = art.key1 and mt.key2 = art.key2 
where art.key1 is not null and art.key2 is not null

Maybe I'm missing a bracket or something but I'm having a hard time deleting rows from a table variable where I'm left joining and looking for the key I'm joining on. If it has a value, then I get rid of it. The problem is that I can't get the query to parse. Any ideas?

declare @MrTemp 
(
    key1 int
   ,key2 int
)

insert into @MrTemp
select key1, key2 from ASourceTable

delete  
from @MrTemp mt
left join ARealTable art on mt.key1 = art.key1 and mt.key2 = art.key2 
where art.key1 is not null and art.key2 is not null

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爺獨霸怡葒院 2024-07-22 21:55:28
DELETE @MrTemp
FROM @MrTemp mt LEFT JOIN ...
DELETE @MrTemp
FROM @MrTemp mt LEFT JOIN ...
寄居者 2024-07-22 21:55:28

您需要在删除之后但在要从中删除的表的 from 之前引用别名

delete art 
from @MrTemp mt left join ARealTable art on 
mt.key1 = art.key1 and mt.key2 = art.key2 
where art.key1 is not null and art.key2 is not null

You need to reference the alias after the delete but before the from for the table you want to delete from

delete art 
from @MrTemp mt left join ARealTable art on 
mt.key1 = art.key1 and mt.key2 = art.key2 
where art.key1 is not null and art.key2 is not null
比忠 2024-07-22 21:55:28

您一次只能从一个表中删除:

从 @MrTemp [ARealTable 中有匹配记录]

delete mt
from @MrTemp mt left join ARealTable art on mt.key1 = art.key1 and mt.key2 = art.key2 where art.key1 is not null and art.key2 is not nu

ll

或删除。
从 ARealTable 中删除 [同样,该记录在 ARealTable 中有对应的记录]

delete art
from @MrTemp mt left join ARealTable art on mt.key1 = art.key1 and mt.key2 = art.key2 where art.key1 is not null and art.key2 is not null

You can only delete from one table at a time:

to delete from @MrTemp [where there is a matching record in ARealTable]

delete mt
from @MrTemp mt left join ARealTable art on mt.key1 = art.key1 and mt.key2 = art.key2 where art.key1 is not null and art.key2 is not nu

ll

or.
to delete from ARealTable [again where the record has a corresponding record in ARealTable]

delete art
from @MrTemp mt left join ARealTable art on mt.key1 = art.key1 and mt.key2 = art.key2 where art.key1 is not null and art.key2 is not null
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文