将数据从一列复制到另一列(在另一个表中)

时间:2022-10-26 08:01:00

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.

我想将联系人表的“BusinessCountry”列复制到tblindiantime表的“CountryName”列。

8 个解决方案

#1


66  

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

在SQL Server 2008中,可以使用以下多表更新:

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:

INSERT INTO tblindiantime (CountryName)
SELECT BusinessCountry FROM contacts

#2


110  

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
    );

#3


16  

Table2.Column2 => Table1.Column1

表二。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
  • 表1 =具有需要更新的列的表
  • table2 = table that has the column with the data
  • 表2 =具有数据列的表
  • column1 = blank column that needs the data from column2 (this is in table1)
  • column1 =需要column2数据的空白列(见表1)
  • column2 = column that has the data (that is in table2)
  • column2 =具有数据的列(如表2所示)

#4


13  

Hope you have key field is two tables.

希望你有两个关键字段。

 UPDATE tblindiantime t
   SET CountryName = (SELECT c.BusinessCountry 
                     FROM contacts c WHERE c.Key = t.Key 
                     )

#5


7  

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.

一个类似的问题的答案对我来说比这个问题的选择答案更正确。使用Mark的答案,我更新的列在所有行中都得到了相同的值(可能是第一行与join匹配的值)。使用来自另一个线程的ParveenaArora的答案,用正确的值更新列。

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):

将Parveena的解决方案转换为使用这个问题的表和列名,查询将如下(我假设这些表是通过tblindiantime.contact_id关联的):

UPDATE tblindiantime
SET CountryName = contacts.BusinessCountry
FROM contacts
WHERE tblindiantime.contact_id = contacts.id;

#6


1  

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

我认为之前所有的答案都是正确的,下面的代码是非常有效的,特别是如果您必须一次更新多个行,请注意:它是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.

我希望这能有所帮助。

#7


0  

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.
  • 更新整个堆栈操作数据库,并使用sql查询创建一个算法来克服这个问题,将其转置到另一个列中。

#8


0  

Now it's more easy with management studio 2016.

现在管理工作室2016更容易了。

Using SQL Server Management Studio

使用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.

1。用要复制的列和要复制的列打开表,右键单击表,然后单击Design。

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

2。单击要复制的列所在的表的选项卡,并选择这些列。

3.From the Edit menu, click Copy.

3所示。在“编辑”菜单中,单击“复制”。

4.Open a new Query Editor window.

4所示。打开一个新的查询编辑器窗口。

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

5。右键单击查询编辑器,然后在编辑器中单击设计查询。

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

6。在“添加表”对话框中,选择源表和目标表,单击“添加”,然后关闭“添加表”对话框。

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

7所示。右键单击该查询编辑器的一个开放区域,指向更改类型,然后单击Insert Results。

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

8。在“插入结果”对话框的“选择目标表”中,选择“目标表”。

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

9。在查询设计器的上部,单击源表中的source列。

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

10。查询设计器现在已经创建了一个插入查询。单击OK将查询放置到原始查询编辑器窗口中。

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

11。执行查询,将数据从源表插入到目标表。

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

https://docs.microsoft.com/en-us/sql/relational-databases/tables/copy-columns-from-one-table-to-another-database-engine的更多信息

#1


66  

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

在SQL Server 2008中,可以使用以下多表更新:

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:

INSERT INTO tblindiantime (CountryName)
SELECT BusinessCountry FROM contacts

#2


110  

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
    );

#3


16  

Table2.Column2 => Table1.Column1

表二。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
  • 表1 =具有需要更新的列的表
  • table2 = table that has the column with the data
  • 表2 =具有数据列的表
  • column1 = blank column that needs the data from column2 (this is in table1)
  • column1 =需要column2数据的空白列(见表1)
  • column2 = column that has the data (that is in table2)
  • column2 =具有数据的列(如表2所示)

#4


13  

Hope you have key field is two tables.

希望你有两个关键字段。

 UPDATE tblindiantime t
   SET CountryName = (SELECT c.BusinessCountry 
                     FROM contacts c WHERE c.Key = t.Key 
                     )

#5


7  

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.

一个类似的问题的答案对我来说比这个问题的选择答案更正确。使用Mark的答案,我更新的列在所有行中都得到了相同的值(可能是第一行与join匹配的值)。使用来自另一个线程的ParveenaArora的答案,用正确的值更新列。

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):

将Parveena的解决方案转换为使用这个问题的表和列名,查询将如下(我假设这些表是通过tblindiantime.contact_id关联的):

UPDATE tblindiantime
SET CountryName = contacts.BusinessCountry
FROM contacts
WHERE tblindiantime.contact_id = contacts.id;

#6


1  

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

我认为之前所有的答案都是正确的,下面的代码是非常有效的,特别是如果您必须一次更新多个行,请注意:它是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.

我希望这能有所帮助。

#7


0  

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.
  • 更新整个堆栈操作数据库,并使用sql查询创建一个算法来克服这个问题,将其转置到另一个列中。

#8


0  

Now it's more easy with management studio 2016.

现在管理工作室2016更容易了。

Using SQL Server Management Studio

使用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.

1。用要复制的列和要复制的列打开表,右键单击表,然后单击Design。

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

2。单击要复制的列所在的表的选项卡,并选择这些列。

3.From the Edit menu, click Copy.

3所示。在“编辑”菜单中,单击“复制”。

4.Open a new Query Editor window.

4所示。打开一个新的查询编辑器窗口。

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

5。右键单击查询编辑器,然后在编辑器中单击设计查询。

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

6。在“添加表”对话框中,选择源表和目标表,单击“添加”,然后关闭“添加表”对话框。

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

7所示。右键单击该查询编辑器的一个开放区域,指向更改类型,然后单击Insert Results。

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

8。在“插入结果”对话框的“选择目标表”中,选择“目标表”。

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

9。在查询设计器的上部,单击源表中的source列。

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

10。查询设计器现在已经创建了一个插入查询。单击OK将查询放置到原始查询编辑器窗口中。

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

11。执行查询,将数据从源表插入到目标表。

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

https://docs.microsoft.com/en-us/sql/relational-databases/tables/copy-columns-from-one-table-to-another-database-engine的更多信息