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 6749633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:42:48+00:00 2026-05-26T12:42:48+00:00

I have a dynamic query to execute. To this query, I receive some parameters

  • 0

I have a dynamic query to execute. To this query, I receive some parameters from user (for example parameters for paging). So I use sp_executesql to execute the query. When I specify order by inside the query, I got an error message says The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.. How can I over come the issue


SET @sql = 'SELECT * FROM
(SELECT ROW_NUMBER() OVER(ORDER BY CaseID)AS RowNumber
,*
FROM [dbo].[CV_CaseMaster]
WHERE SpaceID = @SID
order by @OrderBy
)
AS CV_CaseMasterDetails WHERE RowNumber BETWEEN @MinRow AND @MaxRow'

`


EXEC sp_executesql @sql
                  ,N'@SID UNIQUEIDENTIFIER,@MinRow int, @MaxRow int,@OrderBy VARCHAR(50)'
                  ,@SID=@SpaceID,@MinRow = @CurrentMinRow,@MaxRow = @CurrentMaxRow, @OrderBy =  @SortBy`

OR

Can I specify an Order By column with sp_executesql ?

  • 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-26T12:42:49+00:00Added an answer on May 26, 2026 at 12:42 pm

    You have put order by in the wrong position in the query and you can not use a parameter for order by.

    I guess this will work but it leaves you open for SQL injection.

    SET @sql = 'SELECT * 
                FROM (SELECT ROW_NUMBER() OVER(ORDER BY CaseID) AS RowNumber ,*
                      FROM [dbo].[CV_CaseMaster] 
                      WHERE SpaceID = @SID ) AS CV_CaseMasterDetails 
                WHERE RowNumber BETWEEN @MinRow AND @MaxRow 
                ORDER BY '+@SortBy
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have a really complicated dynamic query that i want to use to
I have problems running a dynamic LIKE statement in my project: this query works
I have tried some ways to use LINQ dynamic queries - LINQKit and LINQ
I have created one dynamic query and it all works well. I execute the
Friends, I have a dynamic sql query that I would like to execute and
I have a dynamic query like this : SET @str_Query = 'SELECT SIM.Item_ID, SIM.Item_Description,
I have a dynamic query that returns around 590,000 records. It runs successfully the
I have a dynamic query which I call and I put the Result set
In my web application, I have a dynamic query that returns huge data to
I need to used dynamic order query in mysql and i have successfully achieved

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.