insert into #table2 (t_col2) select col1 from #table1insert into #table3 (f_col3) select t_col1 from #table2 t2 join #table1 t1 on t1.col1 = t2.t_col2
declare @i intset @i = 0while exists (select * from #table1 where col1 > @i)begin select top 1 @i = col1 from #table1 where col1 > @i insert into #table2 (t_col2) values (@i) insert into #table3 (f_col3) values(SCOPE_IDENTITY())end
create table #table1 (col1 int,col2 varchar(30))insert into #table1 values (1,'martin')insert into #table1 values (2,'tom')insert into #table1 values (3,'schrof')create table #table2(t_col1 int identity(1,1),t_col2 int)create table #table3 (f_col1 int identity(1,1),f_col2 varchar(30),f_col3 int)insert into #table2 (t_col2) output inserted.t_col1,t.col1 into #table3(f_col2,f_col3) select col1 from #table1 t
if object_id('tempdb..#table1') IS NOT NULL DROP TABLE #table1;if object_id('tempdb..#table2') IS NOT NULL DROP TABLE #table2;if object_id('tempdb..#table3') IS NOT NULL DROP TABLE #table3;create table #table1 (col1 int,col2 varchar(30));insert into #table1 values (1,'martin');insert into #table1 values (2,'tom');insert into #table1 values (3,'schrof');create table #table2(t_col1 int identity(1,1),t_col2 int);create table #table3 (f_col1 int identity(1,1),f_col2 varchar(30),f_col3 int);insert into #table2 (t_col2) output inserted.t_col1, inserted.t_col2 into #table3(f_col2,f_col3) select col1 from #table1 t;