473,378 Members | 1,346 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Sending sortExpression as sql command parameter?

JJ
When running an sql stored procedure, is it possible to pass in the
sortexpression as a parameter? When I try it all the other parameters
operate as expected, but the rows are not sorted. The paging works ok, but
not the sorting.
JJ

My data layer has something like:

SqlCommand cmd = new SqlCommand("Stored_Proc_Name", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@LowerBound", SqlDbType.Int).Value = lowerBound;
cmd.Parameters.Add("@UpperBound", SqlDbType.Int).Value = upperBound;
cmd.Parameters.Add("@SortExpression", SqlDbType.NVarChar).Value =
sortExpression;
cn.Open();
The SQL stored proc would be something like:

SELECT * FROM(
SELECT
...Table rows....
ROW_NUMBER() OVER (ORDER BY @SortExpression DESC) AS RowNum
FROM
...TheTable...
WHERE
....SelectionCriteria...
) AllRows
WHERE AllRows.RowNum BETWEEN @LowerBound AND @UpperBound
ORDER BY RowNum ASC
Jun 8 '07 #1
3 5785
JJ
Hmmm I found my answer here
http://www.asp.net/learn/dataaccess/....aspx?tabid=63
parameters are not allowed in 'ORDER BY ...' expression in sql.

JJ

"JJ" <ab*@xyz.comwrote in message
news:e$**************@TK2MSFTNGP04.phx.gbl...
When running an sql stored procedure, is it possible to pass in the
sortexpression as a parameter? When I try it all the other parameters
operate as expected, but the rows are not sorted. The paging works ok, but
not the sorting.
JJ

My data layer has something like:

SqlCommand cmd = new SqlCommand("Stored_Proc_Name", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@LowerBound", SqlDbType.Int).Value = lowerBound;
cmd.Parameters.Add("@UpperBound", SqlDbType.Int).Value = upperBound;
cmd.Parameters.Add("@SortExpression", SqlDbType.NVarChar).Value =
sortExpression;
cn.Open();
The SQL stored proc would be something like:

SELECT * FROM(
SELECT
..Table rows....
ROW_NUMBER() OVER (ORDER BY @SortExpression DESC) AS RowNum
FROM
..TheTable...
WHERE
...SelectionCriteria...
) AllRows
WHERE AllRows.RowNum BETWEEN @LowerBound AND @UpperBound
ORDER BY RowNum ASC

Jun 8 '07 #2

"JJ" <ab*@xyz.comwrote in message
news:e$**************@TK2MSFTNGP04.phx.gbl...
When running an sql stored procedure, is it possible to pass in the
sortexpression as a parameter? When I try it all the other parameters
operate as expected, but the rows are not sorted. The paging works ok, but
not the sorting.
JJ

My data layer has something like:

SqlCommand cmd = new SqlCommand("Stored_Proc_Name", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@LowerBound", SqlDbType.Int).Value = lowerBound;
cmd.Parameters.Add("@UpperBound", SqlDbType.Int).Value = upperBound;
cmd.Parameters.Add("@SortExpression", SqlDbType.NVarChar).Value =
sortExpression;
cn.Open();
The SQL stored proc would be something like:

SELECT * FROM(
SELECT
..Table rows....
ROW_NUMBER() OVER (ORDER BY @SortExpression DESC) AS RowNum
FROM
..TheTable...
WHERE
...SelectionCriteria...
) AllRows
WHERE AllRows.RowNum BETWEEN @LowerBound AND @UpperBound
ORDER BY RowNum ASC
That will not work. The only way would be to pass a value indicating which
column and which direction to sort. You would then have varieties of same
statement in a stored proc which reads the sort parameter and executes the
correct SQL. If you use dynamic SQL you will loose the advantage of SP's
having a stored plan.

LS

Jun 8 '07 #3
"JJ" <ab*@xyz.comwrote in message
news:O%****************@TK2MSFTNGP06.phx.gbl...
Hmmm I found my answer here
http://www.asp.net/learn/dataaccess/....aspx?tabid=63
parameters are not allowed in 'ORDER BY ...' expression in sql.
You can build up your SQL dynamically within the stored procedure and then
run it with sp_executesql:
http://msdn2.microsoft.com/en-us/lib...1(SQL.90).aspx

However, I can't recommend that method because you lose the precompilation /
execution plan advantage of using a stored procedure...

It will, though, allow you to pass in an order by clause as a parameter...
--
http://www.markrae.net

Jun 8 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Fernando Armenta | last post by:
Hello, I am trying to pass the following command ssh IP_address "ssh IP_address ls"
0
by: Andre Azevedo | last post by:
Hi all ! I've created a .net serviced component with only one method. This method receive an ADODB.Command object and execute it. The ADODB.Command object is created in the client process. (VB...
4
by: CoolWriter | last post by:
Hi, While exploring web, I want to send the selected text as a parameter to my application. How can I do this? For instance: When I right click the selected text, my application's name should...
0
by: Manuel Arroba | last post by:
asp.net oledb command parameter error calling an as400 program Hi... I have an as400 program that I can call it directly and it works: variable= "xxxx" sql = "CALL METSIGOP.SIGOPRC ('" +...
0
by: zurg | last post by:
Hi! I have a problem with sending a custom command to a windows service i wrote before. If I use ControlService it works perfectly - the service send an answear... but when I use WMI there's no...
1
by: Khodr | last post by:
Hi everyone, I am calling an Oracle StoredProc that returns a CLOB data type value as an output parameter. What parameter data type should I use? >> I tried adLongVarChar and adVarChar with the...
2
by: Zeya | last post by:
I have an application where I am required to run a full text query. My database is MySQL and code in C#. The way all queries are SELECT * FROM table WHERE firstname = ? and using command...
3
by: Stu Lock | last post by:
Hi, I am trying to write a script that sends a code to a COM port to open a till draw. The code at the bottom of the page (written in C) is what the suppliers sent as an example. I have used the...
9
by: Rob | last post by:
What is the syntax to send a null value as a parameter ? cmd.Parameters.Add("@Year", Convert.ToInt32(strYear)) in cases where strYear = "" I want to send ... cmd.Parameters.Add("@Year",...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.