SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 T-SQL if/else in an ORDER BY clause
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sanctos
Starting Member

19 Posts

Posted - 08/31/2005 :  12:19:05  Show Profile  Reply with Quote
Hi,

I am writing a stored procedure that accepts one value from an ADO Command object. This value determines what to order my query by.

Here is a snippet of the query:


CREATE PROCEDURE dbo.chg_GetCompleted
@intOfficeID int,
@intClientID int,
@intLocationID int,
@dteDateFrom smalldatetime,
@dteDateTo smalldatetime,
@strSort varchar(20)
AS

IF @strSort = 'DateRec'
    BEGIN
	SELECT C.c_ControlID, C.c_DateFrom, C.c_DateTo, C.c_DateReceived, C.c_DateCompleted, C.c_ChgEntID, L.LocName 
	FROM Charges AS C 
	INNER JOIN Locations AS L
	ON L.Loc_ID = C.c_LocationID
	INNER JOIN Clients AS CL 
	ON CL.Clt_ID = L.ClientID 
	INNER JOIN Office AS O 
	ON O.Office_ID = CL.OfficeID 
	WHERE C.c_DateCompleted IS NOT Null
	AND C.c_DateCompleted BETWEEN @dteDateFrom AND @dteDateTo 
	AND C.c_Deleted = 0 
	AND O.Office_ID = @intOfficeID
	AND CL.Clt_ID =  @intClientID
	AND L.Loc_ID = @intLocationID
	ORDER BY C.c_DateReceived DESC
    END


The @strSort variable can be one of three values. Currently in each if/else section, I am writing the same query with the only difference being the ORDER BY field. Instead of writing the same query 3 times I would like to do something like this:


CREATE PROCEDURE dbo.chg_GetCompleted
@intOfficeID int,
@intClientID int,
@intLocationID int,
@dteDateFrom smalldatetime,
@dteDateTo smalldatetime,
@strSort varchar(20)
AS
	SELECT C.c_ControlID, C.c_DateFrom, C.c_DateTo, C.c_DateReceived, C.c_DateCompleted, C.c_ChgEntID, L.LocName 
	FROM Charges AS C 
	INNER JOIN Locations AS L
	ON L.Loc_ID = C.c_LocationID
	INNER JOIN Clients AS CL 
	ON CL.Clt_ID = L.ClientID 
	INNER JOIN Office AS O 
	ON O.Office_ID = CL.OfficeID 
	WHERE C.c_DateCompleted IS NOT Null
	AND C.c_DateCompleted BETWEEN @dteDateFrom AND @dteDateTo 
	AND C.c_Deleted = 0 
	AND O.Office_ID = @intOfficeID
	AND CL.Clt_ID =  @intClientID
	AND L.Loc_ID = @intLocationID

        IF @strSort = 'DateRec'
        BEGIN
	    ORDER BY C.c_DateReceived DESC
        END
        ELSE IF (blah blah.....)


Is there a way I could do this?

sanctos
Starting Member

19 Posts

Posted - 08/31/2005 :  12:40:53  Show Profile  Reply with Quote
Ok, I figured out that this works...


CREATE PROCEDURE dbo.chg_GetCompleted
@intOfficeID int,
@intClientID int,
@intLocationID int,
@dteDateFrom smalldatetime,
@dteDateTo smalldatetime,
@strSort varchar(20)
AS

	SELECT C.c_ControlID, C.c_DateFrom, C.c_DateTo, C.c_DateReceived, C.c_DateCompleted, C.c_ChgEntID, L.LocName 
	FROM Charges AS C 
	INNER JOIN Locations AS L
	ON L.Loc_ID = C.c_LocationID
	INNER JOIN Clients AS CL 
	ON CL.Clt_ID = L.ClientID 
	INNER JOIN Office AS O 
	ON O.Office_ID = CL.OfficeID 
	WHERE C.c_DateCompleted IS NOT Null
	AND C.c_DateCompleted BETWEEN @dteDateFrom AND @dteDateTo 
	AND C.c_Deleted = 0 
	AND O.Office_ID = @intOfficeID
	AND CL.Clt_ID =  @intClientID
	AND L.Loc_ID = @intLocationID
	ORDER BY CASE @strSort
		WHEN 'DateRec' THEN C.c_DateReceived 
		WHEN 'DateComp' THEN C.c_DateCompleted 
		WHEN 'PostID' THEN C.c_ChgEntID 
	END
GO


Now the problem is that the first two selections need to be ordered DESC and the last one ASC

any ideas?
Go to Top of Page

AjarnMark
SQL Slashing Gunting Master

USA
3246 Posts

Posted - 08/31/2005 :  12:44:16  Show Profile  Visit AjarnMark's Homepage  Reply with Quote
You'll need a second CASE statement to set that.

---------------------------
EmeraldCityDomains.com
Go to Top of Page

CLages
Posting Yak Master

Brazil
116 Posts

Posted - 08/31/2005 :  17:02:10  Show Profile  Reply with Quote
CLI_CODIGO AND CLI_CEP ARE INT, cli_nome and cli_uf are String

declare @sort char(10)
set @sort = 'NOME'


select cli_codigo, cli_nome, cli_uf, cli_cep from clientes
ORDER BY CASE @sort
WHEN 'codigo' THEN cli_codigo
WHEN 'nome' THEN cli_nome
WHEN 'uf' THEN cli_uf
WHEN 'cep' THEN cli_cep
END

if i run this script i get
Error converting data type varchar to numeric.

but if i set @sort - codigo works fine

Does Anybody Knows why?

Tks

CLages
Go to Top of Page

TG
Flowing Fount of Yak Knowledge

USA
5469 Posts

Posted - 08/31/2005 :  17:14:46  Show Profile  Reply with Quote
>>Does Anybody Knows why?

Have a look at these comments as well as the original article link. Its more than you ever wanted to know about dynamic ORDER BYs
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=5942

Be One with the Optimizer
TG
Go to Top of Page

SamC
White Water Yakist

USA
3459 Posts

Posted - 08/31/2005 :  17:25:33  Show Profile  Reply with Quote
Another thread with the same question ORDER BY CASE came in today...

http://sqlteam.com/Forums/topic.asp?TOPIC_ID=54622
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000