Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6232317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:05:47+00:00 2026-05-24T10:05:47+00:00

This solution is for an unbounded Gridview paging and having problem with the syntax

  • 0

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)) +
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:05:48+00:00Added an answer on May 24, 2026 at 10:05 am

    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?

    CREATE PROCEDURE dbo.PageSmarter
        @Table     NVARCHAR(128), -- table names should not be varchar(20)
        @FirstRow  INT,
        @PageSize  INT,  
        @TotalRows INT OUTPUT
    AS
    BEGIN
        SET NOCOUNT ON; -- always, in every stored procedure
    
        DECLARE 
            @first_id INT, 
            @startRow INT,
            @sql NVARCHAR(MAX);
    
        SET @sql = N'WITH x AS 
        (
            SELECT
                ID, 
                rn = ROW_NUMBER() OVER (ORDER BY ID) 
            FROM 
                ' + @Table + '
        )
        SELECT rn, ID 
            INTO #x FROM x 
            WHERE rn BETWEEN ' + CONVERT(VARCHAR(12), @FirstRow) 
                + 'AND (' + CONVERT(VARCHAR(12), @FirstRow) 
                + ' +   ' + CONVERT(VARCHAR(12), @PageSize) + ' - 1);
    
        SELECT first_id = MIN(ID) FROM #x;
    
        SELECT
            ID, StringID_from_Master, GUID, short_Text, lang_String, date_Changed,
            prev_LangString, needsTranslation, displayRecord, brief_Descrip
            FROM ' + @Table + ' AS src
            WHERE EXISTS
            (
                SELECT 1 FROM #x
                WHERE ID = src.ID
            );';
    
        EXEC sp_executeSQL @sql;
    
        SELECT @totalRows = SUM(row_count)
            FROM sys.dm_db_partition_stats
            WHERE [object_id] = OBJECT_ID(@Table);
    END
    GO
    
    DECLARE @tr INT;
    EXEC dbo.PageSmarter 'dbo.tablename', 10, 2, @tr OUTPUT;
    SELECT @tr;
    

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This solution works but performance is lower than expected. A query returning 200K rows
I have encountered this solution for a problem I tried to solve (uncheck radio
is this solution possible in asp.net dragging picturebox inside winform on runtime i just
I have this solution for a single button: myButton.Attributes.Add(onclick, this.disabled=true; + GetPostBackEventReference(myButton).ToString()); Which works
I found this solution which works, but I find it hard to believe there
This was just what I was thinking for this solution and I would like
if your app use this solution , do do you plan port the app
I am currently stuck in the design of this solution. The data layer design
Sorry for the Windows developers out there, this solution is for Macs only. This
I doubt I am the only one who has come up with this solution,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.