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
 General SQL Server Forums
 New to SQL Server Programming
 how to branch the where clause?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

allan8964
Posting Yak Master

247 Posts

Posted - 11/15/2012 :  13:44:37  Show Profile  Reply with Quote
Hi there,

I have a problem with WHERE clause branch in a sp. Here is the sample:
1.
select Sum(ProdNum) ... from Table1
where Code = @Code and Left(Model, 4) = 'ABCD' ...
2.
select Sum(ProdNum) ... from Table1
where Code = @Code and Right(Model, 4) = 'ABCD' ...


there are about 100 pieces of statements like those. When the @code values change the different statement is run, for example, if @code='M123' then it runs #1, @code='N321' then it runs #2, so forth. I am thinking is there any way like branch in c# or other better ways to simplify the codes, because the each WHERE clause is long, each select piece takes about 70 lines, or I can only write 100 stored procedures? Really appreciate your help!

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 11/15/2012 :  13:55:22  Show Profile  Reply with Quote
Couple of ways I can think of:

1. Create a look up table - something like this:
CREATE TABLE dbo.CodeLookup
(code CHAR(4))
INSERT INTO dbo.CodeLookup VALUES
('M123'),('N321')


SELECT
	SUM(ProductNum)
FROM
	Table1 t1
	INNER JOIN dbo.CodeLookup c
		ON t1.Model LIKE c.code + '%';


2. Use dynamic SQL. Even though usually one would try to avoid dynamic sql if at all possible, this is one of those cases you might consider it.I prefer the lookup table, but you may need dynamic sql if your join condition changes for each lookup value (as in your example, it is RIGHT(Model,4) in one case and LEFT(Model,4) in another case. There is a discussion of the reasons for doing this and the safe way to do this at Gail Shaw's blog here: http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/
Go to Top of Page

allan8964
Posting Yak Master

247 Posts

Posted - 11/15/2012 :  17:19:07  Show Profile  Reply with Quote
I am not sure about #1 but dynamic sql is something I must try. Thanks for the idea!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 11/15/2012 :  22:20:15  Show Profile  Reply with Quote
quote:
Originally posted by allan8964

I am not sure about #1 but dynamic sql is something I must try. Thanks for the idea!


For first method you just need to create the mapping table as Sunita suggested with values based on your @Code values

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

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.11 seconds. Powered By: Snitz Forums 2000