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 2005 Forums
 Transact-SQL (2005)
 How to determine count(*) result

Author  Topic 

fralo
Posting Yak Master

161 Posts

Posted - 2010-12-14 : 17:39:00
Hi all,

In my stored procedure, I run a select count(*) statement and I wanna find out if it returned > 0 records.

Am I supposed to use ROWCOUNT for this or something else?

This is not working:

select count(*) from table where...

if @@rowcount > 0
.
.
.

Thanks for help.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-12-14 : 17:41:05
select @cnt = count(*) ...

if @cnt > 0

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

fralo
Posting Yak Master

161 Posts

Posted - 2010-12-14 : 17:47:53
Hey, thanks a lot. That worked.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-12-14 : 17:51:54
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-12-20 : 09:25:44
You can effectively turn it to

if exists(select * from table where .......)
.
.


Madhivanan

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

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-12-20 : 10:39:46
That'll only be good if the result of COUNT(*) isn't needed for something. It wasn't clear from the original post.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-12-21 : 07:53:04
quote:
Originally posted by tkizer

That'll only be good if the result of COUNT(*) isn't needed for something. It wasn't clear from the original post.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog


Yes. But I have seen in the past that many people use count(*) to check the existance of the data using the IF clause. So I guessed the OP was doing the same

Madhivanan

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

- Advertisement -