473,386 Members | 1,644 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,386 software developers and data experts.

Two order by's in same stored procedure?

Is it possible to have to ORDER BY statements in the same stored
procedure?

I am trying to use the same stored procedure for two different pages
but the data returned needs to sorted DESC on one page and ASC on
another. Below is my SP:

CREATE procedure sp_getLeads
@p_SortType int,
@p_PropID int
AS
SELECT ID, PropID, Name, Status
FROM Leads
WHERE PropID = @p_PropID
IF @p_SortType = '1'
(ORDER BY DateCreated DESC)
ELSEIF @p_SortType = '2'
(ORDER BY DateCreated ASC)
END
RETURN 1
GO

Can someone tell me what I am doing wrong?
Jul 20 '05 #1
1 3058
You can use the CASE expression for that:

SELECT ID, PropID, Name, Status
FROM Leads
WHERE PropID = @p_PropID
ORDER BY CASE @p_SortType WHEN '1' THEN DateCreated END DESC,
CASE @p_SortType WHEN '2' THEN DateCreated END ASC

Note that I omitted the ELSE clause which by default will evaluate to
NULL.

Theoretically the line the second CASE expression is not necessary and
can be simplified to "DateCreated ASC", but for readability purposes I
also used the a CASE expression there.

Hope this helps,
Gert-Jan
Icarus wrote:

Is it possible to have to ORDER BY statements in the same stored
procedure?

I am trying to use the same stored procedure for two different pages
but the data returned needs to sorted DESC on one page and ASC on
another. Below is my SP:

CREATE procedure sp_getLeads
@p_SortType int,
@p_PropID int
AS
SELECT ID, PropID, Name, Status
FROM Leads
WHERE PropID = @p_PropID
IF @p_SortType = '1'
(ORDER BY DateCreated DESC)
ELSEIF @p_SortType = '2'
(ORDER BY DateCreated ASC)
END
RETURN 1
GO

Can someone tell me what I am doing wrong?

Jul 20 '05 #2

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

Similar topics

4
by: Rick Snagglehimer | last post by:
I keep getting an error from the following SP snippet "Line 11: Incorrect syntax near 'BY'." It runs fine without the ORDER BY clause. >>>>>>>>>>>>>>> WHERE .clientID = .userID
3
by: traceable1 | last post by:
I am inserting data rows into a table via a stored procedure. After the inserts, I query the rows in the table and I want them to spit back out in the same order I put them in. However,...
3
by: Samuel Hon | last post by:
Hi I've done a search and found plenty on ORDER BY problems, but mines hopefully a syntax error. This is a test segment from my stored procedure. DECLARE @Name varchar(50), @SortType Int
7
by: Alex Vorobiev | last post by:
hi there, i am using sql server 7. below is the stored procedure that is giving me grief. its purpose it two-fold, depending on how it is called: either to return a pageset (based on page...
4
by: Dan | last post by:
I've encountered some strange behavior in a recursive procedure I'm writing for a bill of materials. First let me ask directly if what I think is happening is even possible: It seems like the...
3
by: Blake Caraway | last post by:
All, I've seen several posts regarding using UNION or UNION ALL to mash together two or more resultsets into a single result set, but can't seem to find enough info here to help me answer my...
4
by: dave | last post by:
hi all, hope someone can help.... i'm having trouble calling an SP where the ORDER BY operator is specified as a parameter when the SP is called my SP is..... CREATE PROCEDURE...
2
by: Irishmaninusa | last post by:
Hello Everyone, I am populating a dropdown control from a database where the data is datetime values. In my stored procedure I am ordering them by where the most recent is at the top and the...
104
by: Beowulf | last post by:
I have the view below and if I use vwRouteReference as the rowsource for a combo box in an MS Access form or run "SELECT * FROM vwRouteReference" in SQL Query Analyzer, the rows don't come through...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.