..copy data from column A in Table A to column B in Table B?

This is a discussion on "..copy data from column A in Table A to column B in Table B?" within the Databases section. This forum, and the thread "..copy data from column A in Table A to column B in Table B? are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > Databases

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jun 17th, 2005, 20:55
Junior Member
Join Date: May 2005
Location: Virginia US
Age: 30
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
..copy data from column A in Table A to column B in Table B?

Using the SQL UPDATE statement how can I copy a columns data say from column A in Table A to column B in Table B?
Note that I am using Microsoft SQL Server 2000


I try doing the following:

UPDATE B SET B.column_B = A.column_A
FROM Table_A A, Table_B B
WHERE A.numID = B.numID AND A.Line = B.Line

but it did not work so I try this

UPDATE Table_B
SET column_B = (SELECT column_A FROM Table_A WHERE Table_A.numID = 233)
WHERE Table_B.numID = 233

When I do this one I get the following error msg:
Server: Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. The statement has been terminated.


I am trying to copy all the values of column A in Table A to column B in Table B.


Any suggestions or hints are welcome
Reply With Quote

  #2 (permalink)  
Old Jun 17th, 2005, 22:05
Reputable Member
Join Date: Aug 2003
Location: United Kingdom
Posts: 341
Thanks: 0
Thanked 0 Times in 0 Posts
Are you inserting new records into table B based on the values of table A?

I'm fairly sure SELECT INTO is the key to your problem, but I haven't got the code in front of me to provide an answer of any kind.

this might help you out:

http://www.sql-tutorial.net/SQL-SELECT-INTO.asp
Reply With Quote
  #3 (permalink)  
Old Jun 18th, 2005, 15:33
New Member
Join Date: Jun 2005
Location: USA
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to HostBreak
Why don’t you use a procedure instead, Try using loops in procedures.
Reply With Quote
  #4 (permalink)  
Old Jun 21st, 2005, 11:42
Junior Member
Join Date: May 2005
Location: Virginia US
Age: 30
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
THANKS

HostBreak,

Currently I am only trying to do this using SQL statements with the Microsoft SQL Server [SQL Query Analyzer]. Perhaps, in the future but not a the moment. Thanks anyways I'll keep that in mind.



D3mon,

Yes I am inserting new records into table B [column B] based on the values of table A [column A].

What do you mean SELECT INTO is the key to my problem?
you mean that I should use it instead of just select... can you elaborate a little bit more on this.

OK, I just look at the link you gave me and I read some the stuff in it.
I think you are right again.

One of the concerns I have is that it looks like to me that the SQL SELECT INTO statement is use for making backup copies of tables. In my scenario table B already exist with columns that have records, except column B which has <NULL> as a record.
The other concern I have is that when I copy the records in table A [column A] into table B [column B] the values of the superkey of both tables have to match.

Anyways, I'll give it a try. Thanks.
Reply With Quote
  #5 (permalink)  
Old Jun 21st, 2005, 12:23
Junior Member
Join Date: May 2005
Location: Virginia US
Age: 30
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
SQL SELECT INTO statement

D3mon,

The SQL SELECT INTO statement doesn't work if the table already exist.
I try the following sql statement:

SELECT column_A INTO table_B FROM table_A WHERE table_A.numID = 233


and I got the following error from the Microsoft SQL Server 2000:
Server: Msg 2714, Level 16, State 6, Line 1
There is already an object named 'table_B' in the database.


I think my best bet is to try using this SQL statement:
UPDATE table_B
SET column_B = (SELECT column_A FROM table_A WHERE table_A.numID = 233 AND B.numID = 233)
FROM table_B B

But it doesn't work b/c it can only pull one record at a time and it does not like or recognizes the column B.numID
Server: Msg 207, Level 16, State 3, Line 1
Invalid column name 'numID'.
Reply With Quote
  #6 (permalink)  
Old Jun 21st, 2005, 17:50
Reputable Member
Join Date: Aug 2003
Location: United Kingdom
Posts: 341
Thanks: 0
Thanked 0 Times in 0 Posts
I thought the same at first, but I have been able to use it to copy items from a basket table into an exisiting order table, so I'll try to get a copy of the query and post it for you.
Reply With Quote
  #7 (permalink)  
Old Jun 21st, 2005, 17:56
Junior Member
Join Date: May 2005
Location: Virginia US
Age: 30
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
IT WORKS

D3mon,

This SQL UPDATE statement works
UPDATE table_B
SET column_B = (SELECT column_A FROM table_A WHERE table_A.numID = 233 AND B.numID = 233)
FROM table_B B


For some reason the Microsoft SQL Query Analyzer does not like it, so I gave up trying in it and I use a SQL Query standalone application that one of my co-workers created and it work.

It is working... may be the SELECT INTO statement doesn't work in the SQL Query Analyzer too when you have an existing table to where you want to copy the fields, but I am sure it will work on the SQL Query standalone application that one of my co-workers created.


Anyways, Again thank you for all your help.

NOTE: I like your quote "Power is nothing without control", where did you get that?
Reply With Quote
  #8 (permalink)  
Old Jun 21st, 2005, 17:59
Reputable Member
Join Date: Aug 2003
Location: United Kingdom
Posts: 341
Thanks: 0
Thanked 0 Times in 0 Posts
Sorry, it looks Like it was INSERT INTO instead of SELECT INTO. Here's the query I use, perhaps you can modify it for your own purposes:

Code: Select all
INSERT INTO	tblOrderItem 
	
SELECT		@orderID as 'orderid',
		P.productid,
		multiple as 'quantity',
		P.Price as 'price' 
	
FROM 		tblBasketItems
	
INNER JOIN 	tblProducts P 
	ON 	P.ProductID = tblBasketItems.ProductID
		
WHERE 		tblBasketItems.basketID = @basketID
Reply With Quote
  #9 (permalink)  
Old Jun 21st, 2005, 18:02
Reputable Member
Join Date: Aug 2003
Location: United Kingdom
Posts: 341
Thanks: 0
Thanked 0 Times in 0 Posts
"Power is nothing without control"

IIRC it was once the advertising slogan for the Pirelli Tire company. Stuck in my mind ever since I first saw it.
Reply With Quote
  #10 (permalink)  
Old Jun 23rd, 2005, 15:54
New Member
Join Date: Jun 2005
Location: USA
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to HostBreak
Quote:
Originally Posted by D3mon
Sorry, it looks Like it was INSERT INTO instead of SELECT INTO. Here's the query I use, perhaps you can modify it for your own purposes:

Code: Select all
INSERT INTO	tblOrderItem 
	
SELECT		@orderID as 'orderid',
		P.productid,
		multiple as 'quantity',
		P.Price as 'price' 
	
FROM 		tblBasketItems
	
INNER JOIN 	tblProducts P 
	ON 	P.ProductID = tblBasketItems.ProductID
		
WHERE 		tblBasketItems.basketID = @basketID
But I believe that will Insert new records instead of updating the older ones
Reply With Quote
  #11 (permalink)  
Old Jun 23rd, 2005, 18:27
Reputable Member
Join Date: Aug 2003
Location: United Kingdom
Posts: 341
Thanks: 0
Thanked 0 Times in 0 Posts
Hence my original question:

Are you inserting new records into table B based on the values of table A?
Reply With Quote
Reply

Tags
copy, data, column, table

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding a column to a table Jack Franklin Databases 2 Feb 3rd, 2008 08:17
How to automatically 'add column' to table? skuliaxe Databases 1 Jan 22nd, 2008 10:40
Sorting A Table By Column Header NewDesigner Databases 4 Dec 8th, 2006 01:53
ACCESS: one data in one table Monie Classic ASP 1 Aug 2nd, 2004 08:52
Table Column Headings and Page Breaks Trebz Web Page Design 8 Jun 4th, 2004 12:55


All times are GMT. The time now is 12:54.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43