Solved

order by in sql

Posted on 2009-04-02
5
159 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
5 Comments
 
LVL 11

Expert Comment

by:N R
ID: 24054340
can you show your code?
0
 
LVL 41

Accepted Solution

by:
ralmada earned 300 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 76

Assisted Solution

by:arnold
arnold earned 100 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 100 total points
ID: 24054629
ORDER BY
  CASE @SortOrder WHEN 'DESC' THEN StreetName END DESC,
  StreetName ASC
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now