DECLARE @c1= 'where 1=1' create table #t1 table(group_name varchar(50),group_id int,fIndex varchar(50),out int,gain int) set @str = 'insert #t1 SELECT * from t2' + @c1 exec (@str)