更新查询中跳过行

发布于 2025-01-16 21:13:12 字数 919 浏览 0 评论 0原文

我创建了一个更新查询,以使用 Bestel 表中的数据更新 Partij 表中的行。此查询中缺少一件事,希望有人可以帮助我:)

update partij
set Inkprijs1 = (
  select bestel.inkoopprijs
  from bestel
  where partij.levcod= bestel.levcod
  and partij.scancode = bestel.scancode
  and bestel.devisie = 'HARDWA')
,inkprijs2 = (
  select bestel.prijs
  from bestel
  where partij.levcod= bestel.levcod
  and partij.scancode = bestel.scancode
  and bestel.devisie = 'HARDWA')
,PRIJSGRP_1 = (
  select bestel.PRIJSGRP_1
  from bestel
  where partij.levcod= bestel.levcod
  and partij.scancode = bestel.scancode
  and bestel.devisie = 'HARDWA')
,PRIJSGRP_2 = (
  select bestel.PRIJSGRP_2
  from bestel
  where partij.levcod= bestel.levcod
  and partij.scancode = bestel.scancode
  and bestel.devisie = 'HARDWA')

where devisie = 'TESTHW'
   ;

当 Partij 更新函数不存在于表 Bestel 中时,我想跳过它们。当 Partij 表(带有 devsie TESTHW)中存在没有匹配值的行时,根本不应该更新。

希望有人可以帮助我!提前致谢

I've created an update query to update lines in the Partij table with data from the Bestel table. There's one thing missing in this query and hopefully someone can help me :)

update partij
set Inkprijs1 = (
  select bestel.inkoopprijs
  from bestel
  where partij.levcod= bestel.levcod
  and partij.scancode = bestel.scancode
  and bestel.devisie = 'HARDWA')
,inkprijs2 = (
  select bestel.prijs
  from bestel
  where partij.levcod= bestel.levcod
  and partij.scancode = bestel.scancode
  and bestel.devisie = 'HARDWA')
,PRIJSGRP_1 = (
  select bestel.PRIJSGRP_1
  from bestel
  where partij.levcod= bestel.levcod
  and partij.scancode = bestel.scancode
  and bestel.devisie = 'HARDWA')
,PRIJSGRP_2 = (
  select bestel.PRIJSGRP_2
  from bestel
  where partij.levcod= bestel.levcod
  and partij.scancode = bestel.scancode
  and bestel.devisie = 'HARDWA')

where devisie = 'TESTHW'
   ;

I want to skip lines for the Partij update function when they don't excist in the table Bestel. When there are lines in the Partij tabel (with devisie TESTHW) without matching values the shouldn't get updatet at all.

Hopefully someone can help me out! Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

噩梦成真你也成魔 2025-01-23 21:13:12

第一:去掉标量子查询:


update partij p
set Inkprijs1 = b.inkoopprijs
  , inkprijs2 = b.prijs
  , PRIJSGRP_1 = b.PRIJSGRP_1
  , PRIJSGRP_2 = b.PRIJSGRP_2
from bestel b
where p.devisie = 'TESTHW'
and b.devisie = 'HARDWA'
where p.levcod= b.levcod
and p.scancode = b.scancode
   ;

如果 bestel 中存在相应记录,上面的代码只会触及 party

接下来,您可能想要抑制幂等更新:


update partij p
set Inkprijs1 = b.inkoopprijs
  , inkprijs2 = b.prijs
  , PRIJSGRP_1 = b.PRIJSGRP_1
  , PRIJSGRP_2 = b.PRIJSGRP_2
from bestel b
where p.devisie = 'TESTHW'
and b.devisie = 'HARDWA'
where p.levcod= b.levcod
and p.scancode = b.scancode
and (b.inkoopprijs, b.prijs,b.PRIJSGRP_1,b.PRIJSGRP_2) <> (p.Inkprijs1, p.inkprijs2, p.PRIJSGRP_1, p.PRIJSGRP_2)
   ;

最后,您应该引用丑陋的全大写列名(最好避免它们,它们只会造成伤害):


update partij p
set Inkprijs1 = b.inkoopprijs
  , inkprijs2 = b.prijs
  , "PRIJSGRP_1" = b."PRIJSGRP_1"
  , "PRIJSGRP_2" = b."PRIJSGRP_2"
from bestel b
where p.devisie = 'TESTHW'
and b.devisie = 'HARDWA'
where p.levcod= b.levcod
and p.scancode = b.scancode
and (b.inkoopprijs, b.prijs, b."PRIJSGRP_1",b."PRIJSGRP_2") <> (p.Inkprijs1, p.inkprijs2, p."PRIJSGRP_1", p."PRIJSGRP_2")
   ;

First: get rid of the scalar subqueries:


update partij p
set Inkprijs1 = b.inkoopprijs
  , inkprijs2 = b.prijs
  , PRIJSGRP_1 = b.PRIJSGRP_1
  , PRIJSGRP_2 = b.PRIJSGRP_2
from bestel b
where p.devisie = 'TESTHW'
and b.devisie = 'HARDWA'
where p.levcod= b.levcod
and p.scancode = b.scancode
   ;

The above will only touch party if corresponding records in bestel exist.

Next, you may want to supprress idempotent updates:


update partij p
set Inkprijs1 = b.inkoopprijs
  , inkprijs2 = b.prijs
  , PRIJSGRP_1 = b.PRIJSGRP_1
  , PRIJSGRP_2 = b.PRIJSGRP_2
from bestel b
where p.devisie = 'TESTHW'
and b.devisie = 'HARDWA'
where p.levcod= b.levcod
and p.scancode = b.scancode
and (b.inkoopprijs, b.prijs,b.PRIJSGRP_1,b.PRIJSGRP_2) <> (p.Inkprijs1, p.inkprijs2, p.PRIJSGRP_1, p.PRIJSGRP_2)
   ;

Finally, you should quote the ugly ALLCAPS column names (better avoid them, they do only harm):


update partij p
set Inkprijs1 = b.inkoopprijs
  , inkprijs2 = b.prijs
  , "PRIJSGRP_1" = b."PRIJSGRP_1"
  , "PRIJSGRP_2" = b."PRIJSGRP_2"
from bestel b
where p.devisie = 'TESTHW'
and b.devisie = 'HARDWA'
where p.levcod= b.levcod
and p.scancode = b.scancode
and (b.inkoopprijs, b.prijs, b."PRIJSGRP_1",b."PRIJSGRP_2") <> (p.Inkprijs1, p.inkprijs2, p."PRIJSGRP_1", p."PRIJSGRP_2")
   ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文