SQL 将数据复制到同一个表中,并将复制的一列数据的值更改为指定值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9225038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copy data to and from the same table and change the value of copied data in one column to a specified value
提问by flyhai
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:
我想复制 SQL Server 2008 数据库中单个表中的一些数据并将其复制到同一个表中,并将复制数据的一列中的值更改为单个指定数字。这是一个示例,在下表中名为 Metric,数据为:
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:
我想将所有键为 112 的条目复制到 Metric 并将所有复制的行分配一个键为 387,这将表 Metric 中的值提供为:
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.
如何在与 SQL Server 2008 兼容的 SQL 中执行此操作。
Thanks for the help,
谢谢您的帮助,
Tony
托尼
回答by Thit Lwin Oo
Here you try..
在这里你试试..
INSERT INTO Metric(Key,Name,MetricValue)
SELECT 387,Name,MetricValue
FROM Metric
WHERE Key = 112