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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:09:00+00:00 2026-05-13T17:09:00+00:00

I have a stored procedure that receives a string parameter OrderByColumn and builds dynamic

  • 0

I have a stored procedure that receives a string parameter “OrderByColumn” and builds dynamic query accordingly.
This is the part of my stored procedure code:

ROW_NUMBER() OVER (ORDER BY 
  CASE WHEN @OrderByColumn='Date' AND @OrderDirection=0 THEN tbl_Docs.Date END ASC,
  CASE WHEN @OrderByColumn='Count' AND @OrderDirection=0 THEN tbl_Docs.Count END ASC,

And in my code behind function that calls the stores procedure I have:

cmd.Parameters.Add("@OrderByColumn", SqlDbType.NVarChar).Value = orderByColumn;
cmd.Parameters.Add("@OrderDirection", SqlDbType.Int).Value = orderDirection;

The user sets the OrderByColumn parameter by clicking on the gridviews column header, so there is no direct user input, so as I see there is no option to inject any thing…

In the book they also validate the orderByColumn string, I don’t understand why it’s needed because as I’ve noted the user can’t input direct expression.

My question is:

is it safe?

I’ve also read in some book that ORDER BY clause doesn’t support the use of parameters.
What does it mean?

  • 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-13T17:09:01+00:00Added an answer on May 13, 2026 at 5:09 pm

    This seems safe enough to use.

    Im not completely following this section

    I’ve also read in some book that ORDER
    BY clause doesn’t support the use of
    parameters.

    Do you mean the ORDER (ASC/DESC) or column?

    If you are refering to the column, you can achieve this. Something like

    DECLARE @Table TABLE(
            ID INT,
            Val INT
    )
    
    INSERT INTO @Table SELECT 1, 3
    INSERT INTO @Table SELECT 2, 2
    INSERT INTO @Table SELECT 3, 1
    
    DECLARE @FieldNumber INT
    
    SELECT @FieldNumber = 1
    
    SELECT *
    FROM    @Table
    ORdER BY 
                CASE @FieldNumber 
                    WHEN 1 THEN ID
                    WHEN 2 THEN Val
                END
    
    SELECT @FieldNumber = 2
    
    SELECT *
    FROM    @Table
    ORdER BY 
                CASE @FieldNumber 
                    WHEN 1 THEN ID
                    WHEN 2 THEN Val
                END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.