December 28, 2006 at 4:28 pm
I don't use it in this case. It gets used somewhere else.
I was implying that the sp that needs the count of rows doesn't use the data.
Either way, this is beside the point.
December 28, 2006 at 4:47 pm
So, you are adding extra call for SP just to count rows in recordset?
Your company spent too much money on server and now you need to make it occupied?
Don't you think the number of rows could be different in those calls?
What stops you from counting rows where you retrieve the actual recordset?
This way you make sure COUNT will ferlact the actual number of rows in returned recordset.
Why you cannot add OUTPUT parameter to SP to return @@ROWCOUNT?
_____________
Code for TallyGenerator
December 28, 2006 at 4:58 pm
I can see where you are coming from, but it's a little difficult to explain all the WHY's and HOW's of what I am trying to accomplish.
All I want to know is how I can write a SP to ONLY return the number of rows returned by another SP.
I have tried:
exec sp_getAllEmployees
select @@ROWCOUNT
But this returns both the results of the sp_GetAllEmployees sp, as well as the rowcount. I ONLY want the rowcount.
December 28, 2006 at 5:01 pm
December 28, 2006 at 5:06 pm
Perhaps you are correct... but:
At one point my app just needs to know whether there are any employees. It doesn't need to know all the data. Wouldn't it be wrong to pass all the data back to the app if it doesn't need it at this time? Passing lots of unnecessary data is bad practice, is it not?
December 28, 2006 at 5:11 pm
December 28, 2006 at 5:12 pm
So what?
What you gonna do if there are some employees?
Pass the resultset to application?
And if there are no employees you won't pass anything but several bytes overhead for empty recordset.
_____________
Code for TallyGenerator
December 28, 2006 at 5:16 pm
No, these 2 things are used for different things.
I check to see if there are any employees. If there are but pemployee processing is off, I do something like send an email. If there are no employees and employee processing is off, it's ok, nothing needs to be done.
If employee processing is on, I get all the employee data from the 1st sp (sp_GetAllEmployees).
Employee processing can be set to on or off in the config file.
December 28, 2006 at 5:19 pm
And I should mention that sp_getAllEmployees is not simply a select * from a single employee table.
If it was, then I wouldnt have a problem just doing a select count(*) from that table for the 2nd SP.
sp_getAllEmployees contains lots of joins and logic, which is why I don't want to repeat it in the 2nd SP just to get the count.
December 28, 2006 at 5:52 pm
So, you SP is just single SELECT?
That's why you came up with idea of COUNT(*) from SP.
To encapsulate complex joins you must use VIEWs, not SPs.
Then your SP will become :
SELECT {column list}
FROM dbo.AllEmployees -- name of the view
WHERE ... -- if you need to filter something out.
For counting you may use another SP having
SELECT COUNT(*)
FROM dbo.AllEmployees
or use same SP but include parameter in it:
create proc dbo.GetAllEmployees @Rowcount int OUTPUT
AS
IF @Rowcount = 1 -- count only
BEGIN
SELECT @Rowcount = COUNT(*)
FROM dbo.AllEmployees
RETURN
END
SELECT .....
FROM dbo.AllEmployees
SELECT @Rowcount = @@Rowcount
GO
If you call SP with @Rowcount = 1 you've got nothing back, and @Rowcount will contain number of rows in view AllEmployees.
Otherwise you'll get back full resultset, parameter @Rowcount will contain number of rows returned.
But be prepared to be blamed when you "count" run will return not the same number as actual run. Unless you can disconnect all users from that remote database.
_____________
Code for TallyGenerator
December 28, 2006 at 6:40 pm
Thanks for the info. You are absolutely correct.
However, I was using a SP because I need to pass an argument which will be used in the WHERE clause. VIEWS don't take arguments.
Plus sp_GetAllEmployees goes off an gets some data from remote databases through linked servers...
I can't really create a VIEW to get everything and then get the sp to filter it because the VIEW would be very inefficient.
December 28, 2006 at 6:52 pm
Who told you such cr.p about views?
If you do it right view is more efficient than query in SP.
_____________
Code for TallyGenerator
December 28, 2006 at 6:56 pm
I'm just ignorant in that case.
How would I go about it?
BTW can views even get data from linked servers?
December 28, 2006 at 7:03 pm
Okay, you're 100% correct about views. Forgive my ignorance.
Just tested it out (without the linked servers part) and it's working great.
If VIEWs support linked servers, then I'll add that and it will be done.
If not, I can always add that to the sp which is probably more logical anyway.
December 28, 2006 at 7:06 pm
You must create both view and SP using it ON REMOTE SERVER.
And then just call it from here.
_____________
Code for TallyGenerator
Viewing 15 posts - 16 through 30 (of 32 total)
You must be logged in to reply to this topic. Login to reply