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, t.col2 into #table3(f_col2,f_col3) select col1,col2 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 VARCHAR(30));create table #table3 (f_col1 int identity(1,1), f_col2 INT, f_col3 VARCHAR(30));insert into #table2 (t_col2) output inserted.t_col1, inserted.t_col2 into #table3(f_col2,f_col3) select col2 from #table1 t;SELECT * FROM #table2 -- three rowsSELECT * FROM #table3 -- three rows. Column1 = identity column of #Table3, Column2 = identity column of #Table2
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),col3 varchar(30));insert into #table1 values (10,'martin','teacher');insert into #table1 values (20,'tom','trainer');insert into #table1 values (30,'schrof','student');create table #table2(t_col1 int identity(1,1), t_col2 VARCHAR(30));create table #table3 (f_col1 int identity(1,1), f_col2 INT, f_col3 VARCHAR(30),f_col4 VARCHAR(30));--- here in table3.f_col4 i need to store the value of table1.col3---- will output clause work in this scenario?insert into #table2 (t_col2) output inserted.t_col1, inserted.t_col2 into #table3(f_col2,f_col3) select col2 from #table1 t;SELECT * FROM #table2 -- three rowsSELECT * FROM #table3
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), col3 varchar(30));insert into #table1 values (10,'martin','teacher');insert into #table1 values (20,'tom','trainer');insert into #table1 values (30,'schrof','student');create table #table2(t_col1 int identity(1,1), t_col2 VARCHAR(30), [table1_col3] varchar(30));create table #table3 (f_col1 int identity(1,1), f_col2 INT, f_col3 VARCHAR(30), f_col4 VARCHAR(30), [table1_col3] varchar(30));--- here in table3.f_col4 i need to store the value of table1.col3--- will output clause work in this scenario?--- You cannot add random columns from the source table to the OUTPUT column list,--- but you could add a column for it to #Table2 as follows--- You could of course remove it later. insert into #table2 (t_col2, [table1_col3]) output inserted.t_col1, inserted.t_col2, inserted.[table1_col3] into #table3 (f_col2, f_col3, [table1_col3]) select col2, col3 from #table1 t;SELECT * FROM #table2 SELECT * FROM #table3