troubleshooting Question

Multi-table UPDATE with a CASE statement

Avatar of mpdillon
mpdillon asked on
Microsoft SQL Server
6 Comments1 Solution661 ViewsLast Modified:
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

Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 6 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros