If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Sql queries

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-07-12, 08:44
str8uphaitian str8uphaitian is offline
Registered User
 
Join Date: Aug 2012
Posts: 30
Sql queries

Does anyone know the query to copy column data from one table and put it in another
Reply With Quote
  #2 (permalink)  
Old 08-07-12, 09:00
imex imex is offline
Registered User
 
Join Date: Apr 2012
Posts: 178
Try something like that:

Code:
update MyTable
set MyColumn = o.MyColumn
from MyTable as t
join OtherTable as o on o.ID = t.ID
Hope this helps.
Reply With Quote
  #3 (permalink)  
Old 08-07-12, 09:12
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 10,510
Quote:
Originally Posted by str8uphaitian View Post
Does anyone know the query to copy column data from one table and put it in another
why would you want to copy a value from one table to another?
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #4 (permalink)  
Old 08-07-12, 15:00
str8uphaitian str8uphaitian is offline
Registered User
 
Join Date: Aug 2012
Posts: 30
will that query lose my original values? I would like to recreate them without losing my original values.


I want to copy them because I am running a new application that calls for some but not all of the same data columns. As this is a stand alone app I found it to be more concise and practical to have it draw its data from a seperate table
Reply With Quote
  #5 (permalink)  
Old 08-07-12, 15:14
papadi papadi is offline
Registered User
 
Join Date: Oct 2009
Location: 221B Baker St.
Posts: 483
Quote:
will that query lose my original values?
No, the original values will be untouched. Suggest you back up both before experimenting because if you get something crossed, you could update what you do not intend. . .
Reply With Quote
  #6 (permalink)  
Old 08-07-12, 15:18
str8uphaitian str8uphaitian is offline
Registered User
 
Join Date: Aug 2012
Posts: 30
I finally got it. Here was the code that ended up working

UPDATE table1 SET table1.column=table2.somecolumn
FROM table1, table2
WHERE table1.anothercolumn = table2.yetanothercolumn;
Reply With Quote
  #7 (permalink)  
Old 08-08-12, 10:06
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 12,333
Please use JOIN syntax:

UPDATE table1 SET table1.column=table2.somecolumn
FROM table1
inner join table2 on table1.anothercolumn = table2.yetanothercolumn;
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
www.LobsterShot.blogspot.com
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On