我尝试了下面的声明,但它没有用。查询分析器返回“(0)行受影响”。好消息......它没有爆炸......坏消息......我不知道这个陈述有什么问题。
我想我正在使用SQL Server 2000.我还没有在我的演示数据库上尝试过该语句。第二个语句是将旧oillab(oillabid = 4)更新为新的oillab(oillabid = 9)。 oillabid映射到oillabstatus表中正在更改的oillabstatusid。
我想通过customerid更新oildatasetstatus表中的oildatasetstatusid。为了获得customerid,我需要从oildataset表中获取oildatasetid(映射到oildatasetstatus表)和samplepointid。 samplepointid映射到samplepoint表,该表还包含customersiteid。 customersiteid映射到包含customerid的customersite表。更新完成后,我需要更新oildataset表中的oillabid。我猜这必须使用两个语句(如下所示)。这些陈述看起来是正确的还是有任何明显的错误?无论如何在一个声明中做这两个更新?感谢。
update oildatasetstatus set oildatasetstatusid =
case
WHEN oildatasetstatusid = 5 THEN 16
WHEN oildatasetstatusid = 6 THEN 17
WHEN oildatasetstatusid = 7 THEN 18
WHEN oildatasetstatusid = 8 THEN 18
WHEN oildatasetstatusid = 9 THEN 18
WHEN oildatasetstatusid = 10 THEN 19
WHEN oildatasetstatusid = 11 THEN 20
End
where oildatasetid in
(
select oildatasetstatusid from oildatasetstatus
inner join oildataset on oildataset.oildatasetid=oildatasetstatus.oildatasetid
where samplepoinid in
(
select samplepointid from samplepoint where customersiteid in
(
select customersiteid from customersite where customerid=2
)
)
)
update oildataset set oillabid=9 where oildatasetstatusid between 16 and 20
答案 0 :(得分:0)
如何分解并测试您是否获得了正确的结果并逐步将它们组合在一起?
初始UPDATE的WHERE条件(编辑为INNER JOIN而不是SELECT INs)并且还选择了 oildatasetid而不是oildatasetstatusid :
SELECT oildatasetid FROM oildatasetstatus
INNER JOIN oildataset
ON oildataset.oildatasetid = oildatasetstatus.oildatasetid
INNER JOIN samplepoint
ON oildatasetstatus.samplepoinid = samplepoint.samplepointid
INNER JOIN customersite
ON samplepoint.customersiteid = customersite.customersiteid
WHERE customerid = 2;
这应该返回与查询相同的oildatasetid列表,但运行速度更快。
现在UPDATE语句:
UPDATE oildatasetstatus SET oildatasetstatusid =
CASE
WHEN oildatasetstatusid = 5 THEN 16
WHEN oildatasetstatusid = 6 THEN 17
WHEN oildatasetstatusid = 7 THEN 18
WHEN oildatasetstatusid = 8 THEN 18
WHEN oildatasetstatusid = 9 THEN 18
WHEN oildatasetstatusid = 10 THEN 19
WHEN oildatasetstatusid = 11 THEN 20
END
WHERE oildatasetid IN (
SELECT oildatasetid FROM oildatasetstatus
INNER JOIN oildataset
ON oildataset.oildatasetid = oildatasetstatus.oildatasetid
INNER JOIN samplepoint
ON oildatasetstatus.samplepoinid = samplepoint.samplepointid
INNER JOIN customersite
ON samplepoint.customersiteid = customersite.customersiteid
WHERE customerid = 2
);
除非绝对必要,否则我会避免BETWEEN:
UPDATE oildataset SET oillabid = 9 WHERE oildatasetstatusid >= 16 AND oildatasetstatusid <= 20;