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
 how to test if a column contains duplicate values

Author  Topic 

MGA
Starting Member

28 Posts

Posted - 2010-09-28 : 05:41:36
i have a clients table that contains client_id and client_name columns and it contains already data , i want to write a sql command to test if the table has a duplicate values in the client_name field

Sachin.Nand

2937 Posts

Posted - 2010-09-28 : 05:54:53
[code]
select client_id,client_name from table group by client_id,client_name having count(*)>1
[/code]

PBUH

Go to Top of Page

MGA
Starting Member

28 Posts

Posted - 2010-09-28 : 06:05:35
i want to write this sql command in a stored procedure and it should returns true or false only if it had any duplicated value
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-09-28 : 06:18:35
[code]
select client_id,client_name from table group by client_id,client_name having count(*)>1
if @@rowcount>0
return true
else
return false
[/code]



PBUH

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-09-28 : 10:28:21
quote:
Originally posted by Sachin.Nand


select client_id,client_name from table group by client_id,client_name having count(*)>1
if @@rowcount>0
return true
else
return false




PBUH





or

if exists(select client_id,client_name from table group by client_id,client_name having count(*)>1)
return true
else
return false


Madhivanan

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

- Advertisement -