Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Update one column of a table with data from another table


Update one column of a table with data from another table

Author
Message
k-dub
k-dub
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 25
I have two tables which are different with the exception of a single field (column name = "LocationCode").

Both tables can also be joined with a common id field.

I would like to update the "LocationCode" values in one table with the the "LocationCode" of the other table. I can do a relational join on the common id.

It is impractical for me to update one row at a time. How can I update all the rows quickly?
Ramesh Saive
Ramesh Saive
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2564 Visits: 2643

UPDATE A
SET A.LocationCode = B.LocationCode
FROM tableA A INNER JOIN tableB B ON A.id = B.id



--Ramesh


999baz
999baz
SSC Rookie
SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)

Group: General Forum Members
Points: 41 Visits: 260
Hi Ramesh,

Does it matter how big the update is?

I wanna update 2 million + cloumns from one table to the other in a similar manner to your update statement. Is this the correct apprach for such qtys?
Varinder Sandhu
Varinder Sandhu
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 Visits: 35
Dear Friends,


if tableA from database X and TableB form database Y then how can handle this situations

Please reply anyone !!!

Varinder Sandhu,
http://www.varindersandhu.in/
Varinder Sandhu
Varinder Sandhu
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 Visits: 35
found the solutions...

actually if the tables lie in different dbs

then create temp table in db X that contain the data of tableB from db Y

this way our both tables now in same db as X

now simply we can update from one table to another as usual Smile

Varinder Sandhu,
http://www.varindersandhu.in/
ColdCoffee
ColdCoffee
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2317 Visits: 5545
Varinder Sandhu (9/18/2010)
found the solutions...

actually if the tables lie in different dbs

then create temp table in db X that contain the data of tableB from db Y

this way our both tables now in same db as X

now simply we can update from one table to another as usual Smile


When u have data from different DBs, then accessing the tables using three part naming convention will help..
As in:

UPDATE tblA_dbX
SET tblA_dbX.Col2 = tblB_dbY.Col2
FROM dbX.dbo.TableA tblA_dbX -- See the 3-part naming here <DB_Name>.<Schema_Name>.<Table_Name>
JOIN dbY.dbo.TableB tblB_dbY -- See the 3-part naming here <DB_Name>.<Schema_Name>.<Table_Name>
ON tblA_dbX.Col1 = tblB_dbY.Col1



Or, am i missing something here ???
homebrew01
homebrew01
SSCrazy
SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)

Group: General Forum Members
Points: 2980 Visits: 9071
ColdCoffee (9/18/2010)
Varinder Sandhu (9/18/2010)
found the solutions...

actually if the tables lie in different dbs

then create temp table in db X that contain the data of tableB from db Y

this way our both tables now in same db as X

now simply we can update from one table to another as usual Smile


When u have data from different DBs, then accessing the tables using three part naming convention will help..
As in:

UPDATE tblA_dbX
SET tblA_dbX.Col2 = tblB_dbY.Col2
FROM dbX.dbo.TableA tblA_dbX -- See the 3-part naming here <DB_Name>.<Schema_Name>.<Table_Name>
JOIN dbY.dbo.TableB tblB_dbY -- See the 3-part naming here <DB_Name>.<Schema_Name>.<Table_Name>
ON tblA_dbX.Col1 = tblB_dbY.Col1



Or, am i missing something here ???


Sounds right to me. Creating a temp table is an extra step, uses extra space & resources.



Varinder Sandhu
Varinder Sandhu
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 Visits: 35
Dear Friends,

Yes i have checked your way also we can do update with this way...

temp table is temporary table after we should remove this.

Thanks!!!

Varinder Sandhu,
http://www.varindersandhu.in/
gofrancesc
gofrancesc
Say Hey Kid
Say Hey Kid (678 reputation)Say Hey Kid (678 reputation)Say Hey Kid (678 reputation)Say Hey Kid (678 reputation)Say Hey Kid (678 reputation)Say Hey Kid (678 reputation)Say Hey Kid (678 reputation)Say Hey Kid (678 reputation)

Group: General Forum Members
Points: 678 Visits: 812
hi 999baz,

it depends, doing this update you will lock a big chunk of data.
First of all you must test it, use your development DB and test with 10000 rows, 100000 rows, or yours 2500000 rows, and estimate the time needed to complete the task.
Now you know the amount of time needed; have you an adequate time frame to do it without disturb the normal operation?. If yes you can schedule your update; if not you should update your table using a cursor and committing your changes after some amount of rows updated.
Hans K
Hans K
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 17
Hi there, I am also trying to do something like this, but keep getting an error 42000.

My query looks like this:

UPDATE dbo.BlogCopy_copy
SET dbo.BlogCopy_copy.ProcID = dbo.BlogProcedures.ProcID
FROM dbo.BlogCopy_copy, dbo.BlogProcedures
Where dbo.BlogWebs_copy.WebID=dbo.BlogProcedures.WebID



The actual error is : [Err] 42000 - [SQL Server]The multi-part identifier "dbo.BlogWebs_copy.WebID" could not be bound.

I created a new table in BlogCopy_copy for ProcID and I need to have that populated with the same value as the "ProcID" in the table BlogProcedures.

Any help with this will be greatly appreciated.

Thanks
Hans
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search