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.
| Author |
Topic |
|
cfausten
Starting Member
2 Posts |
Posted - 2001-03-11 : 18:49:32
|
| this question involves a stored procedure, designed to support paging through records of a large table, the name of which can be passed in as a variable.I'll include it below, in hopes that someone will be able willing to assist. essentially, the following code segment:.......SET @SQL = 'SELECT COUNT(*) AS MoreRecords FROM '+@TempItems+' WHERE gridrow > ' SET @SQL = @SQL+@LastRecEXEC(@SQL).......produces the following error:.......Syntax error converting the varchar value 'SELECT COUNT(*) AS MoreRecords FROM #wtDCENTRAL WHERE gridrow > ' to a column of data type int........here's the entire procedure (it started out as a sample from the 4guysfromrolla website):.......CREATE PROCEDURE sp_PagedItems_wt_austen ( @TableName varchar(100), @Page int, @RecsPerPage int )AS-- We don't want to return the # of rows inserted-- into our temporary table, so turn NOCOUNT ONSET NOCOUNT ON-- Find out where we will start our records fromDECLARE @RecCount intSELECT @RecCount = @RecsPerPage * @Page + 1--Create a temporary table and fill it with dataDECLARE @TempItems varchar(100)DECLARE @SQL varchar(1000)SET @TempItems = '#'+@TableNameSET @SQL = 'SELECT * INTO '+@TempItems+' FROM '+@TableName+' ORDER BY gridrow'EXEC(@SQL)-- Find out the first and last record we wantDECLARE @FirstRec int, @LastRec intSET @FirstRec = (@Page - 1) * @RecsPerPageSET @LastRec = (@Page * @RecsPerPage + 1)-- Now, return the set of paged records, plus, an indiciation of we-- have more records or not!SET @SQL = 'SELECT COUNT(*) AS MoreRecords FROM '+@TempItems+' WHERE gridrow > ' SET @SQL = @SQL+@LastRecEXEC(@SQL)-- The following select statement is ultimately going to be replaced by the above dynamic SQL SELECT *, MoreRecords = ( SELECT COUNT(*) FROM #TempItems TI WHERE TI.gridrow > @LastRec ) FROM #TempItemsWHERE gridrow > @FirstRec AND gridrow < @LastRec-- Turn NOCOUNT back OFFSET NOCOUNT OFF.......thanks! /a. |
|
|
|
|
|
|
|