sql server - SQL - Insert query Inside another Insert with updated Identity seed -


i have 2 tables t1 , t2

t1 has 10 unique records primary key (identity seed)
t2 has multiple entires foreign key each record in t1

t1 has 2 columns : primarykey - data
t2 has 2 columns : primarykey - foeignkey (this fk primary key of t1)

i need write query select records t1 , insert new entries i.e. t1 ,with same data,and since pk on t1 identity seed auto generate new id, new id generated need join t2 , insert new related records new identity.

i know duplicate data , not concern, 1 time transaction query need not efficient, no cursors please, best if can achieved using select , inserts without doing loops using external variables !
!!

update : if there entry in t1 not suggest in table t2 there has corresponding entry/entries.

p.s. im using sql server 2005

assuming primary key on t2 identity, use:

-- populate t1 insert t1 select data   t1  -- populate t2 t1 values insert t2 select primary_key   t1 x  exists(select null                 t2 y                 join t1 z on z.primary_key = y.foreign_key                z.data = x.data                  , z.primary_key != x.primary_key) 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -