I have a trouble with combining two CTEs. I need to update field_att1 and field_att2 from initial table sku_attribute. Based on the cascade updating I wrote a code (below).
Can anyone advise where the problem is? Error displays that the second CTE (z) doesn't exist.
Thanks in advance.
with
y as
(select
s.ind, s.item_no, s.child, s.skul_no, p.parent,
a.field_att1, a.field_att2
from sku_attribute a, sku s, supp p
where a.skul_no=s.skul_no
and p.skul_no=s.skul_no
and s.ind='F'
)
,
z as
(select
s.ind, s.item_no, s.child, s.skul_no, p.parent,
a.field_att1, a.field_att2
from sku_attribute a, sku s, supp p
where a.skul_no=s.skul_no
and p.skul_no=s.skul_no
and s.ind='N'
)
Update z
set z.field_att1=y.field_att1, z.field_att2=y.field_att2,
from y
where 1=1
and z.parent=y.child
and z.ind='N'
and z.item_no=y.item_no
;
As CTE is not allow to be updated, you can consider the alternative below.
with y as
(
select s.ind, s.item_no, s.child, s.skul_no, p.parent, a.field_att1, a.field_att2
from sku_attribute a, sku s, supp p
where a.skul_no=s.skul_no and p.skul_no=s.skul_no and s.ind='F'
),
z as
(
select s.ind, s.item_no, s.child, s.skul_no, p.parent, a.field_att1, a.field_att2
from sku_attribute a, sku s, supp p
where a.skul_no=s.skul_no and p.skul_no=s.skul_no and s.ind='N'
),
z_update as
(
select coalesce(y.field_att1, z.field_att1) as field_att1,
coalesce(y.field_att2, z.field_att2) as field_att2
from z
left join y on z.parent=y.child and z.ind='N' and z.item_no=y.item_no
)
select * from z_update;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.