![](/img/trans.png)
[英]How to delete multiple rows with two columns as composite primary key in SQL Server?
[英]How to delete multiple rows with 2 columns as composite primary key
例如,我们有以下查询在 Oracle 中有效,但是在 SQL 服务器中无效。 我们如何为 SQL 服务器重写它?
delete from cpi
where (countryid, year) in (('AD', 2010), ('AF', 2009), ('AG', 1992))
相比之下,它要笨重得多,但是您可以使用表值构造函数在EXISTS
子句中创建元组列表并以这种方式匹配它,例如
DELETE FROM cpi
WHERE EXISTS
( SELECT 1
FROM (VALUES ('AD', 2010), ('AF', 2009), ('AG', 1992)) AS v (countryid, year)
WHERE v.countryid = cpi.countryid
AND v.year = cpi.year
);
如果真的只有几对,只需:
delete from cpi
where (countryid = 'AD' and year = 2010) or (countryid = 'AF' and year = 2009) or (countryid = 'AG' and year = 1992)
或者,如果有更多,也许是这样的:
delete from cpi
where countryid + '|' + convert(varchar(4),year) in ('AD|2010','AF|2009','AG|1992')
您可以使用表值构造函数来表达这一点。 . . 在delete
中:
delete from cpi
from (values ('AD', 2010), ('AF', 2009), ('AG', 1992) ) v(countryid, year)
where cpi.countryid = v.countryid and cpi.year = cpi.year;
不需要子查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.