Quickly determine existence of a record | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

Quickly determine existence of a record

Hello, I need to repeatedly determine the existence of a record that has a list of values: client = 1, service date = 1/1/05, etc. I am checking for duplicates based on certain columns. I am using select count(*) from table where client = 1, etc. to get my answers. Are there other ways to accomplish this? Any suggestions are appreciated. Thanks!
Not sure I fully follow. Are you sayin you are looking for a row that contains multiple columns with a speciifc entry your are concerned with or rows with any of the conditions met? What are you wantin to do with these once found?
may be you try this..
select count (*) as frq from table where client = 1 and service date =(….) group by client, service date this should give you a number which you can use for your needs.

If you want to know the duplicate values for the particular column, then use this select column from table group by column having count(*)>1
Madhivanan Failing to plan is Planning to fail
U can use if exists as well
Check this
http://www.sql-server-performance.com/forum/topic.asp?TOPIC_ID=8219
quote:Originally posted by SpeedyZ Hello, I need to repeatedly determine the existence of a record that has a list of values: client = 1, service date = 1/1/05, etc. I am checking for duplicates based on certain columns. I am using select count(*) from table where client = 1, etc. to get my answers. Are there other ways to accomplish this? Any suggestions are appreciated. Thanks!

Hi SpeedyZ,
What i have understood is that u want to check whether a record with certain column values is there in the table or not then i feel u dont need to check count(*). U can try this. set nocount on
select client from table where client=1 and servicedate=’1/1/2005′
if @@rowcount>0 then do something
else do other things.
set nocount off
My favourite for listing duplicates is to use a derived table. Figure out your primary key – all the columns that will determine that a row is a duplicate eg, cola, colb, colc If table is called tableA then to list all duplicates you do: select * from
tableA inner join
(
select cola, colb, colc, count(*) dupcount
from tableA
group by cola, colb, colc
having count(*) > 1
) dups
on tableA.cola = dups.cola and tableA.colb = dups.colb and tableA.colc = dups.colc Dave Small Businesses
www.matiogi.com
If you want to check for existence then:
if exist(select * from table where …)
If you want to check for dups see answers above.
]]>