0

I am trying to pass the stored procedure a value for the Order By clause in the behind code of my asp site. I every time I try to pass it a value that is in the db I get the following error: In correct syntax near Product. Product is a value in the db I want to order by. How can I pass this or any other db value to be sorted by to my stored procedure?

return App_Code.DBHelper.executeDataSetSP(App_Code.DBHelper.getConnection(), "GetOrderDataByDate", 
                                             new SqlParameter[] { new SqlParameter("@start", StartDate),
                                                                  new SqlParameter("@end", EndDate),
                                                                  new SqlParameter("@OrderBy", "Product") }).Tables[0];
Kpt.Khaos
  • 673
  • 3
  • 14
  • 37

1 Answers1

0

I think the answer by dave here Dynamic Sorting within SQL Stored Procedures answers your question.

    SELECT
       name_last,
       name_first,
       CASE @sortCol WHEN 'name_last' THEN [name_last] ELSE 0 END as mySort
    FROM
       table
    ORDER BY 
        mySort
Community
  • 1
  • 1
InbetweenWeekends
  • 1,405
  • 3
  • 23
  • 28