March 15, 2013 at 12:01 pm
Posted - 03/15/2013 : 13:58:23
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)
select @status = 'ERROR,REVIEW,COMPLETE,DELIVERY PREPARED,ALERT'
IF (@status = 'ALL'
AND @AcctName <> 'ALL'
AND @RptName <> 'ALL')
BEGIN
select * from vw_document_main
WHERE ','+REPLACE(@AcctName,'','')+',' LIKE '%,'+acct_name+',%'
AND ','+REPLACE(@RptName,'','')+',' LIKE '%,'+rpt_title+',%'
END
ELSE
BEGIN.......
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...
ORDER BY
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!!
March 15, 2013 at 12:22 pm
This sounds a little bit like a "catch-all" query and little bit like a "one proc to rule them all".
For catch-all queries you should read this article. http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/[/url]
For the one proc to rule them all you should step away quickly. You would not (at least I hope) create a method in a programming language that can do all kinds of various things depending on what you pass in. It would be far better to create separate procs for the different kinds of things you want to return.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply