Solved

Advanced WHERE clause in Stored Procedure for SQL Server 2005.

Posted on 2009-03-31
1
249 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 500 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
In this article I will describe the Backup & Restore 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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

740 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