Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Advanced WHERE clause in Stored Procedure for SQL Server 2005.

Posted on 2009-03-31
1
Medium Priority
?
261 Views
Last Modified: 2012-05-06

Hi expert!

Im using SQL Server 2005 and have question. I have follow SP

CREATE PROCEDURE [dbo].[New_ForTest]
@PlayListID      bigint,
@TypeOrder      smallint
 AS

IF @TypeOrder=1
   SELECT *  FROM  dbo.Events ORDER BY EventID
ELSE
   SELECT *  FROM  dbo.Events ORDER BY ModuleID,EventID

It is about this @TypeOrder. Is there other short way to produce same result ? SOmethink like:

DECLARE @Order
IF @TypeOrder=1
  SET @Order=" ORDER BY EventID"
ELSE
 SET @Order=" ORDER BY ModuleID,EventID"


And then
SELECT *  FROM  dbo.Events + @Order

I need this because order type can be 5-6 ways and I do not want to have different SELECT FOR every of this order by , if I need to change this SQL I will need to do this in all 5-6 SQL which is foolishly ...
0
Comment
Question by:dvplayltd
[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
1 Comment
 
LVL 21

Accepted Solution

by:
JestersGrind earned 2000 total points
ID: 24028390
You could use a CASE statement in the ORDER BY clause.  

SELECT *
FROM dbo.Events
ORDER BY CASE WHEN @TypeOrder <> 1 THEN ModuleID
              END, EventID

Greg


0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

604 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