[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

order by in sql

Posted on 2009-04-02
5
Medium Priority
?
167 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 80

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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

649 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