Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Finding column(s) with unique values

Author  Topic 

mageshks
Yak Posting Veteran

59 Posts

Posted - 2008-12-12 : 07:19:58
Hi all,
How to find the column(s) with unique values in a table

Thanks in advance

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-12-12 : 08:02:51
Not sure what you want

SELECT DISTINCT column_name from table

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-12 : 08:52:02
show some sample data and reqd output to illustrate what you mean by unique values
Go to Top of Page

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2008-12-12 : 09:03:03
You can convert this to a sp and put parameters to your liking,

if exists(Select 1 from sysobjects where name ='tmp')
drop table tmp

declare @cnt int
set @cnt=0
declare @table varchar(100)
set @table='test'--put the name of the table here
declare @i int
set @i=1
declare @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+1
end

Exec (@sql)
Go to Top of Page

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2008-12-12 : 09:08:07
Also, In the above, change the tmp table to a hash table.
Go to Top of Page
   

- Advertisement -