views:

37

answers:

2

I have 2 tables T1 AND T2

T1 has 10 unique records with a primary key (Identity Seed)
T2 has multiple entires with Foreign Key for each record in T1

T1 has 2 columns : PrimaryKey - DATA
T2 has 2 columns : PrimaryKey - FoeignKey (this FK is the Primary Key of T1)

I need to write a query which will select all the records from T1 and INSERT new entries into itself i.e. T1 ,with the same data,and since PK on T1 is identity seed this will auto generate a new ID, as soon as this new ID is generated I need to JOIN the T2 and INSERT new related records with this new Identity.

I know this will duplicate the data and that is not the concern, this is a one time transaction so the query need not be efficient, but NO Cursors please, it would be best if this can be achieved using SELECT and INSERTS without doing loops using external variables !
Thanks !!

UPDATE : If there is an Entry in T1 does not always suggest that in table T2 there has to be a corresponding entry/entries.

P.S. Im using SQL Server 2005

+2  A: 

Assuming that the primary key on T2 is also IDENTITY, use:

-- Populate T1
INSERT INTO T1
SELECT data
  FROM T1

-- Populate T2 with T1 values
INSERT INTO T2
SELECT primary_key
  FROM T1 x
 WHERE EXISTS(SELECT NULL
                FROM T2 y
                JOIN T1 z ON z.primary_key = y.foreign_key
               WHERE z.data = x.data
                 AND z.primary_key != x.primary_key)
OMG Ponies
This can have an impact when T2 might not have an entry for (some) data in T1?
Murtaza RC
@Murtaza: If any of the first batch of 10 records in T1 didn't have a child record in T2, the second query would create such records. If your needs were less abstract, it'd be easier to suggest means that might constraint to rules you didn't mention originally. Otherwise, I don't see the concern if generating dummy data.
OMG Ponies
Thanks - I've just updated the post with the information that T2 might not have entries for all corresponding records in T1 in which case I would have to ignore those records.
Murtaza RC
No - From T1 table everything has to be duplicated, but as far as T2 is concerned only thos record for which the entries exist need to be duplicated - makes sense?
Murtaza RC
@Murtaza: Thank you - that helps. See update.
OMG Ponies
"WHERE y.data = x.data" -- But T2(y) does not have DATA Column ?
Murtaza RC
@Murtaza: Corrected typo
OMG Ponies
Also, I do not see an INSERT for T2 Table, Only a Select statement ?
Murtaza RC
@Murtaza: I need sleep so bad :/ I hope we're close
OMG Ponies
lol Thanks a lot :) This works
Murtaza RC
@Murtaza: Thank goodneZzzzzz =)
OMG Ponies
+1  A: 

If the table doesn't have any current activity on it, something like the following would will exactly duplicate the data. It doesn't have to be dynamic sql - that's just to automate the starting identity seed.

If you have some other data in the tables, you can easily work with this to duplicate that data or join to it.

declare @maxID int

select @maxID = max(pk)
from T1

declare @sql nvarchar(max)
set @sql = 
'create table #T1
(
 new_pk int not null identity(' + cast(@maxID as varchar) + ',1)
 ,old_pk int
 ,data nvarchar(max)
)

insert into #T1 (old_pk, data)
select pk, data
from T1

insert into T1 (data)
select data
from #T1

insert into T2 (fk)
select new_pk
from #T1
inner join T2 on T2.fk = #T1.old_pk

drop table #T1
'

exec sp_executesql @sql
ulty4life
Thanks ! this works like a charm, just wanted something more on the lines of the solution I selected because of avoiding temp tables, but again this is an equally perfect solution :)
Murtaza RC