106

I want to copy data from one column to another column of other table. How can I do that?

I tried the following:

Update tblindiantime Set CountryName =(Select contacts.BusinessCountry From contacts) 

but it did not work.

I want to copy "BusinessCountry" column of contact table to "CountryName" column of tblindiantime table.

TweeZz
  • 4,779
  • 5
  • 39
  • 53
Amit Patil
  • 1,893
  • 4
  • 19
  • 23

8 Answers8

190

Here the query:

Same Table:

UPDATE table_name 
SET column1 = column2

Different Table:

UPDATE table_name1 
    SET column1 = (
        SELECT column2
        FROM table_name2
        WHERE table_name1.id = table_name2.id
    );
Nalan Madheswaran
  • 10,136
  • 1
  • 57
  • 42
  • 13
    Is wrong in the context of the question, where asker specifically involves 2 tables. – bPratik Apr 03 '13 at 14:21
  • 2
    Its working for mysql in case of columns are relates to same table. – Satish Pandey Apr 25 '13 at 07:19
  • 13
    This does however solve my problem, and I got here through the question title which probably matches this answer more than it matches the detail in the question! – Rob Grant Jun 10 '14 at 13:18
  • 6
    This answer answers the title of the question, rather than the question itself. Still, I found this useful for what I was trying to do. – wizard07KSU Mar 19 '15 at 16:15
  • 1
    this is an excellent answer; it helped me to copy only one column from my backup to the main table! – Zelter Ady Jan 20 '18 at 16:29
  • 1
    I don't understand how does it update all columns in the table. Because the subquery returns multiple rows and there is no where condition. – Sree May 20 '19 at 04:11
  • this is worked for me .... UPDATE table_name1 SET column1 = ( SELECT column2 FROM table_name2 WHERE table_name1.id = table_name2.id ); thank you – Bhavin Apr 13 '22 at 10:07
102

In SQL Server 2008 you can use a multi-table update as follows:

UPDATE tblindiantime 
SET tblindiantime.CountryName = contacts.BusinessCountry
FROM tblindiantime 
JOIN contacts
ON -- join condition here

You need a join condition to specify which row should be updated.

If the target table is currently empty then you should use an INSERT instead:

INSERT INTO tblindiantime (CountryName)
SELECT BusinessCountry FROM contacts
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • 1
    Hi thankx for ur rep but it is showing me following error Cannot insert the value NULL into column 'IndianTime', table 'tqms.dbo.tblindiantime'; column does not allow nulls. INSERT fails. The statement has been terminated. – Amit Patil Jul 29 '10 at 11:21
  • 2
    @AmitPatil - I know this is old but may help someone like you. Use ISNULL(contacts.BusinessCountry, '') instead of just contacts.BusinessCountry and it should solve your problem. We are basically replacing null with a empty string. – Ankur-m Sep 18 '12 at 09:26
  • 1
    Because this example doesn't show what a "join condition" might be, also see Michael Pakhantsov's answer, and [cedrikdlb's answer](http://stackoverflow.com/a/25674132/199364), and [Parveen's answer to a similar question, where two columns must be compared to pick the correct row](http://stackoverflow.com/a/17350703/199364). – ToolmakerSteve Dec 09 '16 at 22:53
28

Table2.Column2 => Table1.Column1

I realize this question is old but the accepted answer did not work for me. For future googlers, this is what worked for me:

UPDATE table1 
    SET column1 = (
        SELECT column2
        FROM table2
        WHERE table2.id = table1.id
    );

Whereby:

  • table1 = table that has the column that needs to be updated
  • table2 = table that has the column with the data
  • column1 = blank column that needs the data from column2 (this is in table1)
  • column2 = column that has the data (that is in table2)
Kenny Grage
  • 1,124
  • 9
  • 16
  • 1
    Worked for me - but is same as above answer by `[Michael Pakhantsov]` – Don Cheadle Nov 16 '16 at 21:22
  • 4
    You are right, @mmcra, that it is now the same as the answer above. However, it was not the same at the time that I had written this comment. You will notice that it was edited in Sept and the errors were fixed. I had written this comment in June. – Kenny Grage Nov 16 '16 at 21:31
16

Hope you have key field is two tables.

 UPDATE tblindiantime t
   SET CountryName = (SELECT c.BusinessCountry 
                     FROM contacts c WHERE c.Key = t.Key 
                     )
Marco Lackovic
  • 6,077
  • 7
  • 55
  • 56
Michael Pakhantsov
  • 24,855
  • 6
  • 60
  • 59
  • 2
    this is valid only if you replace one field by one other field it will throw `ORA-01427: single-row subquery returns more than one row` if the select return more than one row, what if I want to select more than one row and update the column with these values? – Muhammad Bekette Mar 07 '13 at 10:05
  • 3
    @Muhammad Ramahy, Do you have nested table in update column? in subquery return several rows, then Key is not PrimaryKey for contacts table. If you have composite primary key you just need conditions to WHERE clause. – Michael Pakhantsov Mar 07 '13 at 15:21
  • 1
    I don't understand what you mean with "Hope you have key field is two tables" – Marco Lackovic Sep 29 '16 at 13:52
  • 2
    @Krige - He apparently means *"If the two tables have a shared Key field to identify which row of each table matches the row of other table"*. If you don't, then you need to change what is after `WHERE`. For example, if tblindiantime has field `ContactID`, which is the `ID` of the row in `contacts` which belongs with each row in tblindiantime, then you would use `WHERE tblindiantime.ContactID=contacts.ID`. See [cedrikdlb's answer](http://stackoverflow.com/a/25674132/199364) for another variation. – ToolmakerSteve Dec 09 '16 at 22:45
10

A similar question's answer worked more correctly for me than this question's selected answer (by Mark Byers). Using Mark's answer, my updated column got the same value in all the rows (perhaps the value from the first row that matched the join). Using ParveenaArora's answer from the other thread updated the column with the correct values.

Transforming Parveena's solution to use this question' table and column names, the query would be as follows (where I assume the tables are related through tblindiantime.contact_id):

UPDATE tblindiantime
SET CountryName = contacts.BusinessCountry
FROM contacts
WHERE tblindiantime.contact_id = contacts.id;
Community
  • 1
  • 1
cedricdlb
  • 689
  • 8
  • 10
3

I think that all previous answers are correct, this below code is very valid specially if you have to update multiple rows at once, note: it's PL/SQL

DECLARE
    CURSOR myCursor IS 
      Select contacts.BusinessCountry 
      From contacts c WHERE c.Key = t.Key;
    ---------------------------------------------------------------------
BEGIN
    FOR resultValue IN myCursor LOOP
        Update tblindiantime t
        Set CountryName=resultValue.BusinessCountry 
        where t.key=resultValue.key;
    END LOOP;
END;

I wish this could help.

Muhammad Bekette
  • 1,396
  • 1
  • 24
  • 60
2

Now it's more easy with management studio 2016.

Using SQL Server Management Studio

To copy data from one table to another

1.Open the table with columns you want to copy and the one you want to copy into by right-clicking the tables, and then clicking Design.

2.Click the tab for the table with the columns you want to copy and select those columns.

3.From the Edit menu, click Copy.

4.Open a new Query Editor window.

5.Right-click the Query Editor, and then click Design Query in Editor.

6.In the Add Table dialog box, select the source and destination table, click Add, and then close the Add Table dialog box.

7.Right-click an open area of the the Query Editor, point to Change Type, and then click Insert Results.

8.In the Choose Target Table for Insert Results dialog box, select the destination table.

9.In the upper portion of the Query Designer, click the source column in the source table.

10.The Query Designer has now created an INSERT query. Click OK to place the query into the original Query Editor window.

11.Execute the query to insert the data from the source table to the destination table.

For More Information https://learn.microsoft.com/en-us/sql/relational-databases/tables/copy-columns-from-one-table-to-another-database-engine

Manojkanth
  • 1,071
  • 11
  • 27
1

It can be solved by using different attribute.

  • Use the cell Control click event.
  • Select the column value that your transpose to anther column.
  • send the selected value to the another text box or level whatever you fill convenient and a complementary button to modify the selected property.
  • update the whole stack op the database and make a algorithm with sql query to overcome this one to transpose it into the another column.