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
 Transact-SQL (2000)
 Help: Paging Database for ASP display

Author  Topic 

whitesword
Starting Member

17 Posts

Posted - 2003-08-02 : 09:51:23
I am having problems with a paging system within my ASP application
I am working off an article from [url]http://www.15seconds.com/issue/010308.htm[/url] entitled "Paging: Use ADO, getrows, or a Stored Procedure?".

I have adapted the SP and it works really well in Query Analyser, but when I call it from my ASP application, it errors (ADODB.Recordset (0x800A0E78) Operation is not allowed when the object is closed.)

Here's my ASP code
strSQL = "EXECUTE dfGetForumSummaryPage @fid=" & oForumID & ", @days=" & fSortDays & ", @page=" & oPageID & _
", @pageSize=" & fPageTopic & ", @sortkey='" & fSortKey & "', @sortorder='" & fSortOrder & "'"
Set rsForum = dbConn.Execute( strSQL )
If NOT rsForum.EOF Then <-- Errors here

If I take that completed statement above and drop it in Query Analyser, it works just fine.

Here's the stored procedure:

CREATE PROCEDURE dfGetForumSummaryPage
@fid INT,
@days INT,
@page INT,
@pageSize INT,
@sortkey VARCHAR(25),
@sortorder VARCHAR(25)
AS
DECLARE @sql AS VARCHAR(200)
DECLARE @start INT
DECLARE @end INT
DECLARE @pageBoundary INT

-- Get our start and end position for our rows
SET @start = ((@page-1) * @pageSize) + 1
IF @@ERROR<> 0
RETURN @@ERROR
SET @end = @start + @pageSize - 1

-- Create our temporary holding table for the recordset
BEGIN TRANSACTION GetDataSet
CREATE TABLE #PostTemp (
RowID int IDENTITY(1,1) PRIMARY KEY,
PostID int
)
IF @@ERROR<>0
GOTO ErrorHandler

-- Insert the required PostIDs for this query
SELECT @sql = 'INSERT INTO #PostTemp (PostID) SELECT PostID FROM dbForumPosts '
SELECT @sql = @sql + 'WHERE (ForumID=' + CONVERT(VARCHAR(4),@fid) + ') AND (Parent=0) AND (deleted=0) AND '
SELECT @sql = @sql + '(LastPostDate > DATEADD(d,' + CONVERT(VARCHAR(4),-@days) + ',GETDATE()))'
SELECT @sql = @sql + ' ORDER BY ' + @sortkey + ' ' + @sortorder

EXEC( @sql )
IF @@ERROR<>0
GOTO ErrorHandler

-- Get our upper bound for the page
SELECT @pageBoundary=MAX(RowID) FROM #PostTemp

-- Check our lower bounds for this page
IF @start > @pageBoundary
GOTO ErrorHandler

-- Check our upper bounds for this page
IF @end > @pageBoundary
SET @end = @pageBoundary

-- Now SELECT the records required
SELECT #PostTemp.PostID, dbo.dbForumPosts.Subject, dbo.dbForumPosts.AuthorID,
dbo.dbUserInfo.Handle, dbo.dbForumPosts.NumViews,
dbo.dbForumPosts.NumReplies, dbo.dbForumPosts.LastPostDate,
dbo.dbForumPosts.ForumID
FROM #PostTemp
INNER JOIN dbo.dbForumPosts ON #PostTemp.PostID = dbo.dbForumPosts.PostID
INNER JOIN dbo.dbUserInfo ON dbo.dbForumPosts.AuthorID = dbo.dbUserInfo.UserID
WHERE ((#PostTemp.RowID >=@start) AND (#PostTemp.RowID <=@end))

DROP TABLE #PostTemp <--Is this necessary?
COMMIT TRANSACTION GetDataSet
RETURN 0

ErrorHandler:
ROLLBACK TRANSACTION GetDataSet
RETURN @@ERROR
GO


Should I get rid of the temporary table and use a normal one? Does the temporary table die when the SP returns?
I am a real newbie to SQL Server, but am loving the learning experience that I am having as it is a whole new and more exciting world than Access, that's for sure

Stoad
Freaky Yak Linguist

1983 Posts

Posted - 2003-08-02 : 12:51:03
Try to add into your procedure body instruction set nocount on (right after 'as').
Without this instruction your procedure returns several recordsets (one per each
select/delete/insert statement) and you get into your rsForum only result of the
very first select statement of your procedure, which is obviously empty.
But with the instruction 'set nocount on' your procedure returns all data sets as a
whole single records set.

PS Also see 'NextRecordset' method of ADODB.Recordset object.

- Vit
Go to Top of Page

Stoad
Freaky Yak Linguist

1983 Posts

Posted - 2003-08-02 : 13:22:47
>DROP TABLE #PostTemp <--Is this necessary?

No. Temp tables created in a sp are dropped automatically after
the sp executed. But...

- Vit
Go to Top of Page

whitesword
Starting Member

17 Posts

Posted - 2003-08-02 : 13:37:20
Thanks, Vit... works like a charm.

quote:
>DROP TABLE #PostTemp <--Is this necessary?
No. Temp tables created in a sp are dropped automatically after
the sp executed. But...


But what???

Leaving that on the end has got me wondering things like.. "What did I do wrong???" or "What's the exception that's gonna break my DB???"

- Roger
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2003-08-02 : 13:39:52
Even though temp tables are automatically dropped when the batch finishes, it is a good practice to explicitly drop them at the end of the procedure.
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-08-02 : 13:43:03
If you're curious, check this out for an alternate method:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=27297&SearchTerms=paging

doesn't use temp tables at all ... a little "different" (many have trouble understanding it apparently ) but I haven't seen a more efficient way to implement paging in SQL server ...

- Jeff
Go to Top of Page

whitesword
Starting Member

17 Posts

Posted - 2003-08-02 : 14:11:53
I am checking it out, actually wasn't that hard to understand once I did a bit of reading of BOL about the SET ROWCOUNT.

I wasn't too keen on the EXEC() bit either, and there was a boo-boo in it.

It's now adapting it to my code that the problem, but that's another post topic.

Cheers
Roger
Go to Top of Page
   

- Advertisement -