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

  • SEARCH
  • Home
  • 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 976483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:45:13+00:00 2026-05-16T03:45:13+00:00

I’ve created following stored procedure in my SQL server 2005 database for general pagination:

  • 0

I’ve created following stored procedure in my SQL server 2005 database for general pagination:

USE [training]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_Pagination1]  
    -- Add the parameters for the stored procedure here 
    @SqlColumns VARCHAR(MAX), 
    @SqlFriendlyColumns VARCHAR(MAX), 
    @SqlTableClause VARCHAR(MAX), 
    @StartRow INT, 
    @EndRow INT, 
    @SqlWhere VARCHAR(MAX), 
    @SqlOuterWhere VARCHAR(MAX), 
    @SqlRowNumOrderBy VARCHAR(MAX), 
    @SqlOuterOrderBy VARCHAR(MAX) 
AS 
DECLARE @rsSQL NVARCHAR(MAX) 
DECLARE @rcSQL NVARCHAR(MAX) 
BEGIN 
    -- SET NOCOUNT ON added to prevent extra result sets from 
    -- interfering with SELECT statements. 
    SET NOCOUNT ON; 
/* 
sample dynamically created SQL: 
    WITH PersonContact AS 
    ( 
        SELECT PC.FirstName, PC.LastName, PC.EmailAddress, 
        ROW_NUMBER() OVER(ORDER BY PC.ContactID) AS RowNumber 
        FROM Person.Contact PC 
    ) 
    SELECT FirstName, LastName, EmailAddress 
    FROM PersonContact 
    WHERE RowNumber > @StartRow AND RowNumber < @EndRow 
    ORDER BY LastName DESC, EmailAddress 
*/ 
    -- build pagination SQL, using StartRow and EndRow to determine 
    -- which results to output 
    SET @rsSQL = N' WITH tempTable AS ( ' +  
        N' SELECT ' +  
            @SqlColumns +  
        N' , ROW_NUMBER() OVER(ORDER BY ' +  
            @SqlRowNumOrderBy +  
        N' ) AS RowNumber ' + 
        N' FROM ' +  
            @SqlTableClause 
    IF @SqlWhere + '' <> '' 
        BEGIN 
            SET @rsSQL = @rsSQL +                   
                N' WHERE ' +  
                    @SqlWhere 
        END 
    SET @rsSQL = @rsSQL + 
        N' ) SELECT ' + 
            @SqlFriendlyColumns + 
        N' FROM tempTable ' + 
        N' WHERE RowNumber >= ' +  
            CAST(@StartRow AS NVARCHAR(32)) +  
        N' AND RowNumber <= ' +  
            CAST(@EndRow AS NVARCHAR(32)) + 
        N' ORDER BY ' + 
            @SqlOuterOrderBy 

    -- uncomment PRINT to debug 
    PRINT @rsSQL 
    EXEC sp_executesql @rsSQL 
    -- build second recordset simple for the count 
    SET @rcSQL =  
            N'SELECT COUNT(*) AS CountAll FROM ' +  
                @SqlTableClause 
    IF @SqlOuterWhere + '' <> '' 
        BEGIN 
            SET @rcSQL = @rcSQL +                   
                N' WHERE ' +  
                      @SqlOuterWhere
        END 

    EXEC sp_executesql  @rcSQL     
    SET NOCOUNT OFF; 
END

Executed procedure with my parameters:

USE [training]
GO

EXEC    [dbo].[usp_Pagination1]
        @SqlColumns = N'tab1.categoryId,tab1.categoryName,tab1.description,tab1.parentCategory,tab2.categoryName AS parentCategoryName',
        @SqlFriendlyColumns = N'categoryId,categoryName,description,parentCategory,parentCategoryName',
        @SqlTableClause = N'vpCategory tab1 LEFT JOIN vpCategory tab2 ON tab1.parentCategory = tab2.categoryId',
        @StartRow = 1,
        @EndRow = 1,
        @SqlWhere = N'tab1.categoryId = 1',
        @SqlOuterWhere = N'categoryId = 1',
        @SqlRowNumOrderBy = N'tab1.categoryId',
        @SqlOuterOrderBy = N'categoryId'

GO

When I execute it for my table vpCategory, I get message :
Msg 209, Level 16, State 1, Line 1
Ambiguous column name ‘categoryId’.

But in result tab it gives me correct output.

ALso message tab gives me following query, that is being built by procedure:

WITH tempTable AS (  SELECT tab1.categoryId,tab1.categoryName,tab1.description,tab1.parentCategory,tab2.categoryName AS parentCategoryName , ROW_NUMBER() OVER(ORDER BY tab1.categoryId ) AS RowNumber  FROM vpCategory tab1 LEFT JOIN vpCategory tab2 ON tab1.parentCategory = tab2.categoryId WHERE tab1.categoryId = 1 ) SELECT categoryId,categoryName,description,parentCategory,parentCategoryName FROM tempTable  WHERE RowNumber >= 1 AND RowNumber <= 1 ORDER BY categoryId

When I execute the above query in new window, it gives me no error!

Can anyone help me out what is going wrong with the stored procedure?

  • 1 1 Answer
  • 1 View
  • 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-16T03:45:14+00:00Added an answer on May 16, 2026 at 3:45 am

    Got It!

    Actually we dot require outer where codition.

    It is:

     ALTER PROCEDURE [dbo].[usp_Pagination]  
        -- Add the parameters for the stored procedure here 
        @SqlColumns VARCHAR(MAX), 
        @SqlFriendlyColumns VARCHAR(MAX), 
        @SqlTableClause VARCHAR(MAX), 
        @StartRow INT, 
        @EndRow INT, 
        @SqlWhere VARCHAR(MAX), 
        @SqlRowNumOrderBy VARCHAR(MAX), 
        @SqlOuterOrderBy VARCHAR(MAX) 
    AS 
    DECLARE @rsSQL NVARCHAR(MAX) 
    DECLARE @rcSQL NVARCHAR(MAX) 
    BEGIN 
        -- SET NOCOUNT ON added to prevent extra result sets from 
        -- interfering with SELECT statements. 
        SET NOCOUNT ON; 
    /* 
    sample dynamically created SQL: 
        WITH PersonContact AS 
        ( 
            SELECT PC.FirstName, PC.LastName, PC.EmailAddress, 
            ROW_NUMBER() OVER(ORDER BY PC.ContactID) AS RowNumber 
            FROM Person.Contact PC 
        ) 
        SELECT FirstName, LastName, EmailAddress 
        FROM PersonContact 
        WHERE RowNumber > @StartRow AND RowNumber < @EndRow 
        ORDER BY LastName DESC, EmailAddress 
    */ 
        -- build pagination SQL, using StartRow and EndRow to determine 
        -- which results to output 
        SET @rsSQL = N' WITH tempTable AS ( ' +  
            N' SELECT ' +  
                @SqlColumns +  
            N' , ROW_NUMBER() OVER(ORDER BY ' +  
                @SqlRowNumOrderBy +  
            N' ) AS RowNumber ' + 
            N' FROM ' +  
                @SqlTableClause 
        IF @SqlWhere + '' <> '' 
            BEGIN 
                SET @rsSQL = @rsSQL +                   
                    N' WHERE ' +  
                        @SqlWhere 
            END 
        SET @rsSQL = @rsSQL + 
            N' ) SELECT ' + 
                @SqlFriendlyColumns + 
            N' FROM tempTable ' + 
            N' WHERE RowNumber >= ' +  
                CAST(@StartRow AS NVARCHAR(32)) +  
            N' AND RowNumber <= ' +  
                CAST(@EndRow AS NVARCHAR(32)) + 
            N' ORDER BY ' + 
                @SqlOuterOrderBy 
    
        -- uncomment PRINT to debug 
        PRINT @rsSQL 
        EXEC sp_executesql @rsSQL 
        -- build second recordset simple for the count 
        SET @rcSQL =  
                N'SELECT COUNT(*) AS CountAll FROM ' +  
                    @SqlTableClause 
        IF @SqlWhere + '' <> '' 
            BEGIN 
                SET @rcSQL = @rcSQL +                   
                    N' WHERE ' +  
                        @SqlWhere 
            END 
    
        EXEC sp_executesql  @rcSQL     
        SET NOCOUNT OFF; 
    END 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a view passing on information from a database: def serve_article(request, id): served_article
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I have a reasonable size flat file database of text documents mostly saved in

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.