Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Update Multiple Fields FROM via a SELECT Statement

This works, but i would like to remove the redundancy. Is there a way to merge the update with a single select statement so i don't have to use vars?

    DECLARE         @OrgAddress1 varchar,         @OrgAddress2 varchar,         @OrgCity varchar,         @OrgState varchar,         @OrgZip varchar,         @DestAddress1 varchar,         @DestAddress2 varchar,         @DestCity varchar,         @DestState varchar,         @DestZip varchar      SELECT          @OrgAddress1    =   OrgAddress,         @OrgAddress2    =   OrgAddress2,         @OrgCity        =   OrgCity,         @OrgState       =   OrgState,         @OrgZip         =   OrgZip,         @DestAddress1   =   DestAddress,         @DestAddress2   =   DestAddress2,         @DestCity       =   DestCity,         @DestState      =   DestState,         @DestZip        =   DestZip     FROM          ProfilerTest.dbo.BookingDetails      WHERE          MyID=@MyID      UPDATE SHIPMENT     SET         OrgAddress1     =   @OrgAddress1,         OrgAddress2     =   @OrgAddress2,         OrgCity         =   @OrgCity,         OrgState        =   @OrgState,         OrgZip          =   @OrgZip,         DestAddress1    =   @DestAddress1,         DestAddress2    =   @DestAddress2,         DestCity        =   @DestCity,         DestState       =   @DestState,         DestZip         =   @DestZip     WHERE          MyID2=@ MyID2 
like image 633
KellCOMnet Avatar asked Aug 12 '09 18:08

KellCOMnet


People also ask

How do you UPDATE multiple columns in a single UPDATE statement in SQL?

The UPDATE statement in SQL is used to update the data of an existing table in database. We can update single columns as well as multiple columns using UPDATE statement as per our requirement. UPDATE table_name SET column1 = value1, column2 = value2,...

Can you UPDATE multiple fields in SQL?

We can update multiple columns by specifying multiple columns after the SET command in the UPDATE statement. The UPDATE statement is always followed by the SET command, it specifies the column where the update is required.

How do you UPDATE multiple variables in SQL?

To update multiple columns use the SET clause to specify additional columns. Just like with the single columns you specify a column and its new value, then another set of column and values. In this case each column is separated with a column.

How do you UPDATE multiple values in one query?

UPDATE config SET t1. config_value = 'value' , t2. config_value = 'value2' WHERE t1. config_name = 'name1' AND t2.


1 Answers

Something like this should work (can't test it right now - from memory):

UPDATE SHIPMENT SET   OrgAddress1     = BD.OrgAddress1,   OrgAddress2     = BD.OrgAddress2,   OrgCity         = BD.OrgCity,   OrgState        = BD.OrgState,   OrgZip          = BD.OrgZip,   DestAddress1    = BD.DestAddress1,   DestAddress2    = BD.DestAddress2,   DestCity        = BD.DestCity,   DestState       = BD.DestState,   DestZip         = BD.DestZip FROM    BookingDetails BD WHERE     SHIPMENT.MyID2 = @MyID2    AND    BD.MyID = @MyID 

Does that help?

like image 189
marc_s Avatar answered Sep 25 '22 03:09

marc_s