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
 Old Forums
 CLOSED - General SQL Server
 Returning Multiple Recordsets

Author  Topic 

RoLYroLLs
Constraint Violating Yak Guru

255 Posts

Posted - 2006-01-19 : 11:36:59
Hello all. I've been up and down Google and searched this site as well, but haven't run into any 'good' information on this issue.

The heart of the issue is paging, which I did find a good article for paging in SQL 2000 a while back, and just recently one for SQL2005, which supports paging.

Anyhow here's the situation. When I've come across paging, I always used client-side paging (using ASP) since I was naive about doing it at the SQL (server-side) level. Now I want to optimize my apps to take advantage of this server-side paging. I can successfully only get the records I want from SQL, but my issue is this. Say I want 10 records per page...fine. But without returning the total number of records, how do I know how many 'more' records are there in order to display a next button, or maybe display all the possible pages available with the page size? I've successfully writen a sproc that first returns a recordset with the records, and then another select returning the total count, but I get an ASP error, when I use "set rs= rs.NextRecordset" saying "ADODB.Recordset (0x800A0CB3) Current provider does not support returning multiple recordsets from a single execution". I've also tried returning the value as a returned value, and an output value, but it seems the issue lies somewhere else.

By the error message itself, I'm sure I can go ahead and execute another sproc/query to return the count, but I'm trying to determine a way to do it with a single execution. I've tried using the SQLOLEDB provider, using an SQL Native Client DSN connection. I'm all outta ideas. Unless it I'm going wrong about it and the provider the error speaks of is ADODB.

Thanks everyone.

- RoLY roLLs

jhermiz

3564 Posts

Posted - 2006-01-19 : 13:53:57
quote:
Originally posted by RoLYroLLs

Hello all. I've been up and down Google and searched this site as well, but haven't run into any 'good' information on this issue.

The heart of the issue is paging, which I did find a good article for paging in SQL 2000 a while back, and just recently one for SQL2005, which supports paging.

Anyhow here's the situation. When I've come across paging, I always used client-side paging (using ASP) since I was naive about doing it at the SQL (server-side) level. Now I want to optimize my apps to take advantage of this server-side paging. I can successfully only get the records I want from SQL, but my issue is this. Say I want 10 records per page...fine. But without returning the total number of records, how do I know how many 'more' records are there in order to display a next button, or maybe display all the possible pages available with the page size? I've successfully writen a sproc that first returns a recordset with the records, and then another select returning the total count, but I get an ASP error, when I use "set rs= rs.NextRecordset" saying "ADODB.Recordset (0x800A0CB3) Current provider does not support returning multiple recordsets from a single execution". I've also tried returning the value as a returned value, and an output value, but it seems the issue lies somewhere else.

By the error message itself, I'm sure I can go ahead and execute another sproc/query to return the count, but I'm trying to determine a way to do it with a single execution. I've tried using the SQLOLEDB provider, using an SQL Native Client DSN connection. I'm all outta ideas. Unless it I'm going wrong about it and the provider the error speaks of is ADODB.

Thanks everyone.

- RoLY roLLs



What about returning an output parameter from your stored procedure with the count of those records?




Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]

RS Blog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-01-19 : 14:02:34
If the server-side paging that you are using is using the temp table approach, then you just need to include the count in your temp table. Have it be the first column. Here is an example pulled from one of our stored procedures:


Select
totalRecords = (Select count (distinct assetID) from #TempItems),
AssetID,
FROM
#TempItems
Where ID > @FirstRec AND ID < @LastRec
Order By ID ASC


I had to remove a bunch of stuff from the query in order to post it here. But it should give you an idea.



Tara Kizer
aka tduggan
Go to Top of Page

RoLYroLLs
Constraint Violating Yak Guru

255 Posts

Posted - 2006-01-19 : 15:46:13
Here's my sproc, no temp tables:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER PROCEDURE [dbo].[proc_ShowClient]
@Page int
, @PageSize int
as
declare
@PageStart int
, @PageEnd int

set @PageStart = (@PageSize * @Page) - @PageSize + 1;
set @PageEnd = @PageSize * @Page;

begin
select
Client.ClientID
, dbo.DeFormatGUID(Client.ClientUID) as ClientUID
, Client.ClientAccountNumber
, Client.ClientCompany
, Client.ClientFirstName
, Client.ClientMiddleName
, Client.ClientLastName
from
(
select top (@PageEnd)
a.*
, ROW_NUMBER() over(order by a.ClientCompany) as TheCount
from
Client a
order by
a.ClientCompany) Client
where
Client.TheCount between @PageStart and @PageEnd

end

I've tried adding @Count int output as a param and setting select @Count = count(*) from Client. But no go, it seems like the error is with ADO? Anyhow I'm wondering if anyone has come across this issue or what are other methods others use to return the total number of records.

I am tempted to use something like returning the number of rows within the recordset as a field like this:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER PROCEDURE [dbo].[proc_ShowClient]
@Page int
, @PageSize int
as
declare
@PageStart int
, @PageEnd int
, @Count int

set nocount on --is this correct what i do here, setting nocount on?
select @Count = count(Client.ClientID) from client
set nocount off


set @PageStart = (@PageSize * @Page) - @PageSize + 1;
set @PageEnd = @PageSize * @Page;

begin
select
@Count as TotalCount
, Client.ClientID
, Client.ClientUID
, Client.ClientAccountNumber
, Client.ClientCompany
, Client.ClientFirstName
, Client.ClientMiddleName
, Client.ClientLastName
from
(
select top (@PageEnd)
a.*
, ROW_NUMBER() over(order by a.ClientCompany) as TheCount
from
Client a
order by
a.ClientCompany) Client
where
Client.TheCount between @PageStart and @PageEnd

end



- RoLY roLLs
Go to Top of Page

RoLYroLLs
Constraint Violating Yak Guru

255 Posts

Posted - 2006-01-19 : 15:48:55
Ha, thanks Tara, my solution is the same as yours =) Are there any performance issues here? I'd think not, but then again, I'm not an SQL Guru like all of you

- RoLY roLLs
Go to Top of Page

RoLYroLLs
Constraint Violating Yak Guru

255 Posts

Posted - 2006-01-19 : 15:56:27
I noticed, Tara, that your solution was one that I was going to do, but I changed it a bit, because I was concerned with performance and I'll explain what and where. take these two sprocs: (the differences are in bold)
#1
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER PROCEDURE [dbo].[proc_ShowClient]
@Page int
, @PageSize int
as
declare
@PageStart int
, @PageEnd int
, @Count int

set nocount on --is this correct what i do here, setting nocount on?
select @Count = count(Client.ClientID) from client
set nocount off


set @PageStart = (@PageSize * @Page) - @PageSize + 1;
set @PageEnd = @PageSize * @Page;

begin
select
@Count as TotalCount
, Client.ClientID
, Client.ClientUID
, Client.ClientAccountNumber
, Client.ClientCompany
, Client.ClientFirstName
, Client.ClientMiddleName
, Client.ClientLastName
from
(
select top (@PageEnd)
a.*
, ROW_NUMBER() over(order by a.ClientCompany) as TheCount
from
Client a
order by
a.ClientCompany) Client
where
Client.TheCount between @PageStart and @PageEnd

end


versus #2
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER PROCEDURE [dbo].[proc_ShowClient]
@Page int
, @PageSize int
as
declare
@PageStart int
, @PageEnd int

set @PageStart = (@PageSize * @Page) - @PageSize + 1;
set @PageEnd = @PageSize * @Page;

begin
select
(select count(Client.ClientID) from client) as TotalCount
, Client.ClientID
, Client.ClientUID
, Client.ClientAccountNumber
, Client.ClientCompany
, Client.ClientFirstName
, Client.ClientMiddleName
, Client.ClientLastName
from
(
select top (@PageEnd)
a.*
, ROW_NUMBER() over(order by a.ClientCompany) as TheCount
from
Client a
order by
a.ClientCompany) Client
where
Client.TheCount between @PageStart and @PageEnd

end


Since I don't know the technical ways SQL retrieves each row, would #1 be better performace (even if just slightly) than #2? Does #2 run the nested select for EACH row generated? Even if it's optimized does it run for each time, or not? I think it does, which is why I like #1 better. I'm looking for all sides of the arguement so I can decide for myself which is best for what situation, so feel free to tell me what you think. Thanks!

- RoLY roLLs
Go to Top of Page

RoLYroLLs
Constraint Violating Yak Guru

255 Posts

Posted - 2006-01-19 : 15:58:51
BTW Tara, been a while since I've been on here, just noticed ur name changed! Congrats on your marriage (I know it was back in Oct, but I wasn't invited :P)! Best wishes!

- RoLY roLLs
Go to Top of Page
   

- Advertisement -