flyhai
flyhai

Reputation: 251

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

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

Upvotes: 25

Views: 42836

Answers (1)

Thit Lwin Oo
Thit Lwin Oo

Reputation: 3448

Here you try..

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

Upvotes: 43

Related Questions