如何使用冲突数据更新状态,其中化学温度 table 在温度 table #temp 上具有相同的化学 ID?

How to update status with conflict data where chemical temp table have same chemical Id on temp table #temp?

如何使用冲突数据更新状态 where chemicalId on temp table #chemical have same chemical Id on temp table #temp ?

实现该目标的步骤

1- 获取与临时存在的部件相关的部件 table #temp 在临时 table 上具有相同的掩码 ID #parts

在这种情况下,结果将是

PartId  MaskId   ChemicalId
200     1000     901
500     1700     909
600     1700     909

2- 检查温度 table #chemical for partid 或相同 maskId 的相关零件 ID

如果步骤 1 中的 chemicalid 与温度上的 chemicalid 不同 table 化学 然后状态没有任何反应。

如果 checmicalid 在步骤 1 上与温度相同的 chemicalid table 化学品 然后状态将根据零件 ID 更新为冲突。

这里 200 在温度 table #temp 和化学温度 table 上有 901 化学 ID 100 那么状态将是冲突的,因为它与相同的掩码 ID 相关并且具有相同的化学 ID 901。

这里 700 在温度 table #temp 和化学温度 table 上有 909 化学 ID 500 和 600 那么状态将没有任何改变,因为它与相同的掩码 ID 相关并且具有不同的化学 ID 920。

create table #temp
(
partid     int,
maskid     int,
chemicalid int,
status  nvarchar(50)
)
insert into #temp
values
(100,1000,901,null),
(700,1700,909,null)

create table  #parts
(
PartId  int,
maskId  int
)

insert into #parts (PartId,maskId)
values
(100,1000),
(200,1000),
(500,1700),
(600,1700),
(700,1700)


create table  #Chemical
(
Chemicalmasterid  int,
ChemicalId  int,
PartId   int
)                        
insert into  #Chemical(Chemicalmasterid,ChemicalId,PartId)
values
(1 ,901,100),
(7 ,920,700)

final result
100     1000 901 conflict chemical id
700     1700 909     null

我试过的

update t set status ='conflict chemical id' from #temp t

这个逻辑比较复杂,不过你给了样例数据和中间结果。

我想我明白你想做什么。我认为这个查询可以做到:

update t
    set status ='conflict chemical id'
    from temp t join
         parts p
         on p.maskid = t.maskid and
            p.partid <> t.partid join
         chemical c
         on c.partid = t.partid
     where exists (select 1 from chemical c where c.partid = t.partid and c.chemicalid = t.chemicalid);

Here 是一个 db<>fiddle.