Link to home
Start Free TrialLog in
Avatar of mpdillon
mpdillon

asked on

Multi-table UPDATE with a CASE statement

I need to update one table with the a value from one of two tables depending on a case statement. The table to be updated is the Hdr table. I want to update a field called Fld in the Hdr table. When the value of the ShipToNo in the Hdr table is blank then Fld is equal to the Zip from the CusFil. The Hdr table and the CusFile Table are Joined by Hdr.CusNo = CusFil.CusNo. When the ShipToNo in the Hdr is not blank then Hdr.Fld is equalt to the Zip from the ShipFil. The ShipFil and Hdr tables are joined by Hdr.ShipToNo = ShipFil.ShipToNo.
Here is my attempt to start the update statement.

Update Hdr Set Hdr.Fld =  (Case When Hdr.ShipToNo = '' Then ?Cusfil.Zip else ?ShipFil.Zip end)
Your help in filling in the ?'s would be greatly appreciated.
My guess
?CusFil.CusNo = Select Zip From CusFil where Hdr.CusNo = CusFil.Cusno

?ShipFil.ShipToNo = Select Zip From ShipFil where Hdr.ShipToNo = ShipFil.ShipToNo

Thanks
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Update Hdr Set Hdr.Fld = Case When Hdr.ShipToNo = '' Then CusFil.CusNo Else ShipFil.ShipToNo End From Hdr Inner Join CusFil On Hdr.CusNo = CusFil.CusNo Inner Join ShipFil On Hdr.ShipToNo = ShipFil.ShipToNo

Perhaps.
Avatar of rattu976
rattu976

Please see if you can adapt the following to your cause.

update hdr
set fld = case when h.ShipToNo IS NULL THEN c.Zip
            ELSE s.Zip END
FROM hdr h inner join CusFil c on h.CusNo = c.CusNo
inner join ShipFil s on h.ShipToNo = s.ShipToNo
where h.(pk_field) = hdr.(pk_field)

Regards,
Rehan
Avatar of Racim BOUDJAKDJI
Assuming you '' means NULL..this should do...


update Hdr
Set Hdr.Fld = B.zip2
from hdr A
inner join
(
select Hdr.zip zip1, CustFil.zip zip2 from Hdr
inner join Custfil
on Hdr.CusNo = CusFil.CusNo
where ShipToNo is null
) B
on B.zip1 = A.zip


update Hdr
Set Hdr.Fld = B.zip2
from hdr A
inner join
(
select Hdr.zip zip1, ShipFil.zip zip2 from Hdr
inner join ShipFil
on Hdr.ShipToNo = ShipFil.ShipToNo
where ShipToNo is not null
) B
on B.zip1 = A.zip

if '' means something else than NULL then just replace the inner query conditions IS NULL/IS NOT NULL by something that describes best the ''.    


Hope this helps....

Actually it would be better to run...

update Hdr
Set Hdr.Fld = B.zip2
from hdr A
inner join
(
select Hdr.zip zip1, ShipFil.zip zip2 from Hdr
inner join ShipFil
on Hdr.ShipToNo = ShipFil.ShipToNo
where ShipToNo is not null
) B
on B.zip1 = A.zip

update Hdr
Set Hdr.Fld = B.zip2
from hdr A
inner join
(
select Hdr.zip zip1, CustFil.zip zip2 from Hdr
inner join Custfil
on Hdr.CusNo = CusFil.CusNo
where ShipToNo is null
) B
on B.zip1 = A.zip


Do not forget to do a backup before running any update...
ASKER CERTIFIED SOLUTION
Avatar of mpdillon
mpdillon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial