我有一个 PurchaseStationeryDetails 和 TVP(表值参数)TVP_PurchaseStationeryDetails ,我的问题是 - >我想删除记录来自 TVD表中无法使用的 PurchaseStationeryDetails 。
declare @TVP_PurchaseStationeryDetails as TVP_PurchaseStationeryDetails
请注意我将数据插入 TVP表
DELETE from PurchaseStationeryDetails
WHERE PurchaseStationeryMasterId = 1003 AND PurchaseStationeryDetailsId IN
(SELECT PSD.PurchaseStationeryDetailsId FROM PurchaseStationeryDetails PSD, @TVP_PurchaseStationeryDetails TVP_PSD
WHERE PSD.PurchaseStationeryMasterId = TVP_PSD.PurchaseStationeryMasterId AND PSD.PurchaseStationeryDetailsId ! = TVP_PSD.PurchaseStationeryDetailsId AND TVP_PSD.PurchaseStationeryDetailsId != 0)
PurchaseStationeryDetails ---> PurchaseStationeryDetailsId,PurchaseStationeryMasterId .....等列
TVP_PurchaseStationeryDetails ---> PurchaseStationeryDetailsId,PurchaseStationeryMasterId .....等列
这里我想删除记录 - >来自PurchaseStationeryDetails表的 104,2
答案 0 :(得分:0)
像这样尝试smth
delete from PSD
from PurchaseStationeryDetails PSD
where not exists (
select 1 from @TVP_PurchaseStationeryDetails TVP_PSD
where PSD.PurchaseStationeryMasterId = TVP_PSD.PurchaseStationeryMasterId
and isnull(PSD.PurchaseStationeryDetailsId,0) = TVP_PSD.PurchaseStationeryDetailsId
)
and PSD.PurchaseStationeryMasterId = 1003
答案 1 :(得分:0)
尝试使用Left
或Right Outer Join
create table #tvp (id int, val int)
insert into #tvp
values
(101, 1), (102, 2),(103,2)
create table #purchase (iden int, value int)
insert into #purchase
values
(101, 1), (102, 2),(103,2),(104,2)
delete p
from #tvp t
right outer join
#purchase p
on (t.val = p.value and t.id = p.iden)
where t.id is null