Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 append and update at same time?

Author  Topic 

newbie123
Starting Member

2 Posts

Posted - 2012-08-13 : 12:33:52
Table Name: Relation
Data:
ID ID1 ID2
1 602 5
1 602 402
1 602 403
1 602 407
1 602 491
1 602 492
1 602 493
2 602 2
2 602 402
2 602 403
2 602 407
2 602 491
2 602 492
2 602 493
...

What I would like to do is append the following data to the same table , they are same as those record with ID=2, the only difference is ID=3
3 602 2
3 602 402
3 602 403
3 602 407
3 602 491
3 602 492
3 602 493

Is there a SQL statement can achieve that? I tried insert into and update but not successful.

Appreciate for any help :)
newbie

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-08-13 : 12:42:33
Run the parts starting at SELECT in the following first. That will show you exactly what is going to be inserted into the table. Then if you are satisfied, run the whole statement:
INSERT INTO Relation
SELECT
3 AS ID,
ID1,
ID2
FROM
Relation
WHERE
ID = 2;
Go to Top of Page

newbie123
Starting Member

2 Posts

Posted - 2012-08-13 : 13:59:44
Works, thx!
Go to Top of Page
   

- Advertisement -