?
Solved

order by in sql

Posted on 2009-04-02
5
Medium Priority
?
165 Views
Last Modified: 2012-05-06
Hi Expert,

I used
order by StreetName + ' ' + @SortOrder in store procedure,
@SortOrder  passed

then i execute store procedure
ABC 'desc'

It doesn't work.

Thanks in advance
It doesn't work



0
Comment
Question by:dshi15
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 12

Expert Comment

by:Nathan Riley
ID: 24054340
can you show your code?
0
 
LVL 41

Accepted Solution

by:
ralmada earned 1200 total points
ID: 24054355

you can do something like this:
order by CASE when @sortOrder = 'ASC' then StreetName ASC else StreetName DESC END
0
 
LVL 79

Assisted Solution

by:arnold
arnold earned 400 total points
ID: 24054365
If you want to order based on several columns, you either need to use order by column1 asc, column2.
Alternatively, you might need to group the item in the select (column1 + ' ' + clomn2 ) as newColumn order by newColumn.
0
 
LVL 41

Expert Comment

by:ralmada
ID: 24054423
If not you will have to use dynamic SQL. so the logic you need in your sp should look like this

declare @strSQL varchar(2000)
 
set @strSQL = 'select field1, field2 from yourtable order by StreetName ' + @sortOrder
 
exec(@strSQL)

Open in new window

0
 
LVL 22

Assisted Solution

by:dportas
dportas earned 400 total points
ID: 24054629
ORDER BY
  CASE @SortOrder WHEN 'DESC' THEN StreetName END DESC,
  StreetName ASC
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question