Copy data from one column to other column (which is in a different table)

后端 未结 8 1429
小鲜肉
小鲜肉 2020-11-30 20:59

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          


        
相关标签:
8条回答
  • 2020-11-30 21:28

    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;
    
    0 讨论(0)
  • 2020-11-30 21:33

    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.
    0 讨论(0)
  • 2020-11-30 21:39

    Hope you have key field is two tables.

     UPDATE tblindiantime t
       SET CountryName = (SELECT c.BusinessCountry 
                         FROM contacts c WHERE c.Key = t.Key 
                         )
    
    0 讨论(0)
  • 2020-11-30 21:43

    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
        );
    
    0 讨论(0)
  • 2020-11-30 21:51

    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.

    0 讨论(0)
  • 2020-11-30 21:52

    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
    
    0 讨论(0)
提交回复
热议问题