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

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 If....else in stored procedure

Author  Topic 

Pinto
Aged Yak Warrior

590 Posts

Posted - 2012-04-18 : 06:10:55
I have 3 dropdownlists Town, Area, Zone which allow the user to choose parameters to select records. They can select one, two or three parameters. I have another 3 dropdownlists where they can select the values they want to change the selected records to.

eg ddl1 is selected - value is Broadway. All records with Broadway in Town field are selected.
In ddl1New user selects 'Johnstown'. SP is called which changes all selected records from Broadway to Johnstown. Area and Zone are left unchanged because values weren't in ddl2New or ddl3New

User can select (ddl1 and ddl2)or (ddl1 and ddl3) or (ddl2 and ddl3) or any one individually as parameters. Similarly, they can select the same combinations in the new ddls. If a field is left blank in ddl1 2 or 3 then the selected fields must have these blank too, but if a new value in the New ddls is selected the blank will be changed to the new value.

I started using if ....else in sp but it's getting out of control - is there another easier way ?

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-04-18 : 07:00:55
Looks like what you need is what they call a "catch-all query". Take a look at the description and example that Gail Shaw has in her blog here: http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/ Even though she is talking about products and order ids and transaction types, conceptually it is the same as the problem you are trying to solve.

You may also want to look at these blogs:
http://www.sommarskog.se/dyn-search-2008.html
http://aboutsqlserver.com/2010/08/29/sunday-t-sql-tip-how-to-select-data-with-unknown-parameter-set/

If you still have difficulty getting it to work, post the DDL for your tables and the query you currently have along with some sample data, and I am sure people on the forum would be able to help.
Go to Top of Page
   

- Advertisement -