This solution is for an unbounded Gridview paging and having problem with the syntax of this query:
> @currTable varchar(20),
@startRowIndex int,
@maximumRows int,
@totalRows int OUTPUT
AS
DECLARE @first_id int, @startRow int
IF @startRowIndex = 1
SET @startRowIndex = 1
ELSE
SET @startRowIndex = ((@startRowIndex - 1) * @maximumRows)+1
SET ROWCOUNT @startRowIndex
DECLARE @sql varchar(250);
SET @sql = 'SELECT ID, StringID_from_Master, GUID, short_Text, lang_String, date_Changed, prev_LangString, needsTranslation, displayRecord, brief_Descrip FROM ' + @currTable + ' ';
EXECUTE(@sql);
PRINT @first_id
SET ROWCOUNT @maximumRows
SELECT @sql = 'SELECT ' + CAST(@first_id as varchar(20)) + ' = ID FROM ' + QUOTENAME(@currTable) + ' ORDER BY ID ' ;
EXEC (@sql);
SET ROWCOUNT 0
-- Get the total rows
SET @sql = 'SELECT ' + + CAST(@totalRowsas varchar(20)) + ' = COUNT(ID) FROM ' + @currTable + ' ';
EXECUTE(@sql);
RETURN
<
The errors is:
Conversion failed when converting the varchar value ”SELECT ‘ to data type int.
Tried also
nvarchar and varchar. = + CAST(@first_id as varchar(10)) +
If you’re trying to implement paging, this is wrong in so many ways. First, you’re using SET ROWCOUNT to limit to @startRowIndex, but then you’re selecting ALL n rows (with no ORDER BY), then getting the first ID, then counting the total rows by selecting from the table? Might I suggest a better approach?
I haven’t tested all edge cases with this specific implementation. I will confess, there are much better ways to do this, but they usually aren’t complicated with the additional requirement of dynamic table names. This suggests that there is something inherently wrong with your design if you can run the exact same queries against any number of tables and get similar results.
In any case, you can review some of the (quite lengthy) discussion about various approaches to paging over at SQL Server Central:
http://www.sqlservercentral.com/articles/T-SQL/66030/
There are 62 comments following up on the article:
http://www.sqlservercentral.com/Forums/Topic672980-329-1.aspx