views:

3298

answers:

4

Title says it all - is it possible to do this? I have some filters set in my source Stored Procedure and I really don't want to have to duplicate it in another just to get the rowcount.

A: 

can you give an example?? You could call the sproc and just do a count on the results?

Robert
Yes, that's exactly what I want to do. How do I do that?
Mike C.
+3  A: 

The only way I know how to do this is to insert into a temp table from the stored procedure and then select the count. Unfortunately, there's no pretty way to perform a "select" on a stored procedure.

CREATE TABLE #stuff (id int, status char(6))
INSERT #stuff (id, status)
EXEC dbo.sp_get_stuff

SELECT count(*) FROM #stuff

DROP TABLE #stuff

Edit

The above method will allow you to select from a stored procedure, but as Greg pointed out, a rowcount can be simplified to:

EXEC dbo.sp_get_stuff
SELECT @@Rowcount
Gavin Miller
I think this is what I was looking for. Let me give it a whirl...
Mike C.
Why use a temp table when @@ROWCOUNT works fine, with practically zero performance overhead, and is much simpler to use?
Greg Beech
@Greg - You're correct
Gavin Miller
@Greg - Is there a way to return only the rowcount instead of returning both the results and the rowcount? I'm trying to fine tune this as much as possible. Thanks!
Mike C.
+2  A: 

This also works:

create proc pTest1
as
select * from comp
go

create proc pTest2
as
exec pTest1
select @@rowcount
GO
edosoft
Is there a way to return only the rowcount instead of returning both the results and the rowcount? I'm trying to fine tune this as much as possible. Thanks!
Mike C.
Not easily no, you can catch the resultset in a local table like LFSR said. See also here http://stackoverflow.com/questions/605996/ms-sql-suppress-return-value-of-stored-procedure-called-in-stored-procedure
edosoft
what does "go" in the above code mean ?
anjanb
GO usualy marks the end of a block of sql code. Force of habit I guess
edosoft
A: 

If you are really trying to fine tune as much as possible, then you will have to change the source stored procedure. If you are looking at performance, then returning the rowset just to get the count is not something to even consider.

Darian Miller
You're right. I had tunnel vision.
Mike C.
Been there, done that!
Darian Miller