加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql-server – Sql server DELETE和WITH子句

发布时间:2020-12-12 06:36:58 所属栏目:MsSql教程 来源:网络整理
导读:我需要构建一个SQL语句来从某个表中删除与另一个select语句匹配的记录. 在Teradata我们使用 delete from table1 where (col1,col2) in ( select col1,col2 from table2) 在SQL Server中,不允许在WHERE..IN子句中包含多于一列的列.我以为我可以使用WITH子句:
我需要构建一个SQL语句来从某个表中删除与另一个select语句匹配的记录.

在Teradata我们使用

delete from table1 
where (col1,col2) in (
  select col1,col2
  from table2
)

在SQL Server中,不允许在WHERE..IN子句中包含多于一列的列.我以为我可以使用WITH子句:

with tempTable(col1,col2) as (
select col1,col2
from table2
)
delete from table1
where table1.col1 = tempTable.col1
and table1.col2 = tempTable.col2

如何使用WITH..DELETE子句?还有另一种方式吗?

解决方法

这应该这样做:
DELETE Table1
 from Table1 t1
  inner join tempTable t2
   on t2.Col1 = t1.Col1
    and t2.Col2 = t1.Col2

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读