Copy data to and from the same table and change the value of copied data in one column to a specified value

非 Y 不嫁゛ 提交于 2019-12-08 14:35:27

问题


I want to copy some data in a single table in a SQL Server 2008 database and copy it into the same table and change the values in one column of the copied data to a single specified number. Here is an example, in the following table called Metric, the data is:

Key  Name    MetricValue
112    Joe      34
112    Fred     38
112    Frank    12
112    John     56
112    David    87
112    Sue      43
234    Alli     34
234    Susan    38
234    Anne     12
234    Franki   56

I want to copy all those entries with a key of 112 to Metric and assign all copied rows a Key of 387, this gives the values in the table Metric as:

Key  Name    MetricValue
112    Joe      34
112    Fred     38
112    Frank    12
112    John     56
112    David    87
112    Sue      43
234    Alli     34
234    Susan    38
234    Anne     12
234    Franki   56
387    Joe      34
387    Fred     38
387    Frank    12
387    John     56
387    David    87
387    Sue      43 

Note, this table also has a primary key which I have not shown above.

How can I do this in SQL that is compatible with SQL Server 2008.

Thanks for the help,

Tony


回答1:


Here you try..

INSERT INTO Metric(Key,Name,MetricValue)
SELECT 387,Name,MetricValue
FROM Metric
WHERE Key = 112


来源:https://stackoverflow.com/questions/9225038/copy-data-to-and-from-the-same-table-and-change-the-value-of-copied-data-in-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!