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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 count in a top 2

Author  Topic 

marioab
Starting Member

2 Posts

Posted - 2007-03-16 : 08:00:15
Hi,

i need to do a select on which i have to process one record just when the result of this select returns me only one row, if the select returnme more than one this selection will be rejected. I have tried to do something like:

select count(*) from (select top 2 * from table1 where condition)

but it was not successful. I need this query have a really good performance also.

Is this possible? Thanks in advance.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-16 : 08:32:06
select count(*) from (select top 2 * from table1 where condition) as d


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

marioab
Starting Member

2 Posts

Posted - 2007-03-16 : 09:06:00
Thanks so much!!! it works. Is this the best way to get data? or there is a better solution to improve the performance? Thanks again!!!
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-16 : 09:19:02
I think this might be a little faster. I haven't tested it.[code]
SET ROWCOUNT 2

declare @items int
SELECT @Items = COUNT(*) from table1 where {condition}

SET ROWCOUNT ON

if @items <> 1
return

-- do real stuff here


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -