Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Using ORDER BY with multiple IF ELSE statements
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

6 Posts

Posted - 03/15/2013 :  13:58:23  Show Profile  Reply with Quote
Hello all,

I've created a (probably over complex) query that will eventually allow a user to select a value from 3 different variables. @Status, @AcctName, @RptName.

Since I'm allowing the end user to multi-select different values from the variables, my query begins to become a bit more complex...

For instance:

declare @status varchar(150)

IF (@status = 'ALL'
AND @AcctName <> 'ALL'
AND @RptName <> 'ALL')
select * from vw_document_main
WHERE ','+REPLACE(@AcctName,'','')+',' LIKE '%,'+acct_name+',%'
AND ','+REPLACE(@RptName,'','')+',' LIKE '%,'+rpt_title+',%'

Since I'd have to allow for customization I have 9 different IF ELSE statements.

What I need help with doing is creating an ORDER BY variable for the entire IF ELSE statement.

I.E - @ColName = 'system_Status' then the entire IF ELSE statement would Order by System Status...

Any idea?

I began writing a query...

CASE @ColName
WHEN 'system_Status' THEN system_status
WHEN 'document_status_code' THEN document_status_code

However, I'm not sure where this ORDER BY clause would go into my IF ELSE statement... would it need to be after each individual SELECT statement?

Any clarification would help, thanks!!

Yak Posting Veteran

62 Posts

Posted - 03/15/2013 :  14:03:17  Show Profile  Reply with Quote
Yes it has to be in each SELECT statment. So put Order by after
AND ','+REPLACE(@RptName,'','')+',' LIKE '%,'+rpt_title+',%'

and similarly in the Select in ELSE....

You can always use PRINT to see the query and then execute that in query analyzer to make sure the dynamic query is correct.

Edited by - SQLNOVICE999 on 03/15/2013 14:04:14
Go to Top of Page

Flowing Fount of Yak Knowledge

4614 Posts

Posted - 03/15/2013 :  14:13:39  Show Profile  Reply with Quote
I'm not sure what your performance is like with your current set of queries or how much of a concern that is. However, you might be able to use a catach-all query ( something like:
select * 
from vw_document_main
		','+REPLACE(@AcctName,'','')+',' LIKE '%,'+acct_name+',%'
		OR @AcctName = 'ALL'
		','+REPLACE(@RptName,'','')+',' LIKE '%,'+rpt_title+',%'
		OR @RptName = 'All'

Another option is go to dynamic sql, that should gain you more performance and possibly be easier to read.

Edited by - Lamprey on 03/15/2013 14:14:21
Go to Top of Page
  Previous Topic Topic Next 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.03 seconds. Powered By: Snitz Forums 2000