Hi
I want to copy N number of records within a table which is easy with a INSERT SELECT however after the insert I want a list showing oldId and newId. So lets say I have a SELECT statement which returns:
ID | Name
3 | Test A
4 | Test B
5 | Test C
If I do a SELECT INTO from this it inserts 3 new PKs. I want a list showing the new and old Ids e.g.
3 | 49
4 | 50
5 | 51
I know how to do this with loops, cursors, Scope_Identity(), or temp tables etc with many lines of code but was wondering if there is a cleaver way to get the new/old list in one line of SQL after the INSERT SELECT e.g.
INSERT INTO tbl(Name)
SELECT Name
FROM tbl
WHERE blah = 1
.
SELECT New_Old_Ids()
And I can't join the table to itself because "Name" is not unique so I'm looking for some sort of built in SQL feature. It's probably not possible but thought I'd ask.
I'm on SQL Server 2005.
Cheers Matthew