You can convert this to a sp and put parameters to your liking,if exists(Select 1 from sysobjects where name ='tmp')drop table tmpdeclare @cnt intset @cnt=0declare @table varchar(100)set @table='test'--put the name of the table heredeclare @i intset @i=1declare @sql nvarchar(2000)set @sql=''select @cnt=count(*) from syscolumns where [id]=object_id(@table)select IDENTITY(int, 1,1) as a,'if not exists(select ['+ name +'],count(*) from '+@table +' group by ['+ name +'] having count(*)>1) select '''+name+''''+' ' as b into tmp from syscolumns where [id]=object_id(@table)while (@i<=@cnt)begin select @sql=@sql+b from tmp where a=@i set @i=@i+1endExec (@sql)