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.
| Author |
Topic |
|
Dargon
Starting Member
26 Posts |
Posted - 2008-07-30 : 15:26:51
|
| Is it any way to set conditional WHERE clause? I'll try to explain what I need to do.I have table Products(ProductID,ProductName, Qty) which defines products in stock.I also have another table ProductBounds(ProductID, LBound, UBound) which defines upper and lower range of Quantity for each productProductsID ProductName Qty1 Cheese 11.22 Beer 1003 Meat 334 Candy -114(yes, it is possible to have negative inventory )ProductBoundsProductID LBound UBound1 3 202 10 663 5 144 20 200I know that to get products which are overstocked I need to write something likeSelect * from Products pinner join ProductBounds pb on p.ID=pb.ProductIDwhere Qty>UboundBut I have stored procedure with input parameter @mode which tells in what range we need to get products.Create stored procedure GetProducts@mode varchar(10)asIF (@mode ='negative')beginSelect * from Products pinner join ProductBounds pb on p.ID=pb.ProductIDwhere Qty<0endIF(@mode='low')beginSelect * from Products pinner join ProductBounds pb on p.ID=pb.ProductIDwhere Qty<=pb.LBoundendIF(@mode='OK')beginSelect * from Products pinner join ProductBounds pb on p.ID=pb.ProductIDwhere Qty>pb.LBound and Qty<pb.UBoundendIF(@mode='high')beginSelect * from Products pinner join ProductBounds pb on p.ID=pb.ProductIDwhere Qty>pb.UBound endI hate to repeat almost identical select statements. Is it any way to refactor the SP? I tried to set conditional WHERE clause, but no luck so far. Thanks for help, I am really puzzled.Does anyone have any thoughts? Dargon |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
|
Dargon
Starting Member
26 Posts |
Posted - 2008-07-31 : 09:57:19
|
| Oh, thanks! You saved my life. I can not imadgine , how ugly my query would be. I may need a lesson in Boolean logic;)Dargon |
 |
|
|
|
|
|