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 |
|
extrapulp
Starting Member
9 Posts |
Posted - 2010-06-14 : 09:32:45
|
I am fairly new to SQL. I have a stored procedure that is working currently. I would like to expand it to handle each input differently. It is being called from a WebApp.Here is the current working stored procedureprocedure dbo.SeriesSelect ( @Series char)asIf (@Series = 'F') Begin Select PartNum,Min,Max,Available,SalesSample,PendingSale,Notes,SeqNum from dbo.DomeStock Where PartNum Like '%'+ @Series + '%' and PartNum not Like '%D' + @Series + '%' and PartNum not Like '%N' + @Series + '%' and PartNum not Like '%C' + @Series + '%' Order by PartNum EndElseSelect PartNum,Min,Max,Available,SalesSample,PendingSale,Notes,SeqNum from dbo.DomestockWhere PartNum Like '%' + @Series + '%'Order by PartNum I would like to add a case statement instead of the if statement. Every way I have tried it gives me syntax errors. I have triedSelect Case @SeriesWhen 'F' ThenAnd Select CaseWhen @Series = 'F' ThenAnd Select Case When (@Series = 'F') ThenI would like to end up with the functionality of something like this.Case @SeriesWhen 'F' Then Select statement for 'F'When 'S99' Then Different select statement for 'S99'When 'BL' Then Different select statement for 'BL'ElseDefault select statementAny suggestions are greatly appreciated!Thanks |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-06-14 : 09:47:39
|
| Are number of columns different for each case?MadhivananFailing to plan is Planning to fail |
 |
|
|
extrapulp
Starting Member
9 Posts |
Posted - 2010-06-14 : 11:02:15
|
| madhivanan,No the columns returned are exactly the same and in the same order. It is the search criteria that i need to tweak. I am doing a Like lookup. As in the example if statement. If we are looking for 'F' I need to filter out the records that have 'DF', 'NF', and 'CF'. I just want to refine the search results for each series that the user can select. To make it easier on the Java programmer and to keep some standards I would like all the data related logic in stored procedures.The users are able to select the following choices. F, U, NS, P, M, C, RK, A, WT, XF, DT, BL, GX, Q, S99, E, Dart, and PNP. That is the exact text that is being passed to the stored procedure. In instance of the Dart selection the PartNum will be similar to A4085-310. But the data being passed to the stored procedure is 'Dart'.Here is a sampling of the data that I am returning if it will help.Min Max PartNum Available SalesSample PendingSale Notes0 0 E12-RDF-340 8830 0 0 0 E12.7-RDF-400 5940 0 0 0 E12.7-RDFT-280 4610 0 0 0 E12.7-RDFT-400 6060 0 20005000 25000 F08210 20440 0 1000 10000 F08210N 7810 0 0 0 PNP-08X2 (F08400) 0 0 0 0 PNP-08 (F08400N) 60 0 0 0 PNP-08 (UD08290) 0 0 0 0 PNP-08 (UD08290N) 0 0 0 0 PNP-08 (NS08400) 0 0 20000 50000 F08260 1890 0 00 0 F08260C 4500 (1500 per Cart) Thanks for the quick reply! |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-06-15 : 02:10:01
|
| Is this not working?Select PartNum,Min,Max,Available,SalesSample,PendingSale,Notes,SeqNum from dbo.DomestockWhere PartNum Like '%' + @Series + '%'Order by PartNumMadhivananFailing to plan is Planning to fail |
 |
|
|
extrapulp
Starting Member
9 Posts |
Posted - 2010-06-15 : 10:20:17
|
| madhivnan,Yes that query works. The issue is that I need to create 18 separate select statements depending on the value passed into @Series. I can create the queries. I can not seem to create the needed case statement to be able to process the different queries.Thanks |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-06-15 : 10:24:54
|
Something like this Begin Select PartNum,Min,Max,Available,SalesSample,PendingSale,Notes,SeqNum from dbo.DomeStock Where ( PartNum Like '%'+ @Series + '%' and PartNum not Like '%D' + @Series + '%' and PartNum not Like '%N' + @Series + '%' and PartNum not Like '%C' + @Series + '%' and (@Series = 'F') ) OR ( PartNum Like '%' + @Series + '%' and (@Series = 'D') ) OR ( PartNum Like '%' + @Series + '%' and (@Series = 'S39') ) Order by PartNum End MadhivananFailing to plan is Planning to fail |
 |
|
|
extrapulp
Starting Member
9 Posts |
Posted - 2010-06-15 : 11:40:05
|
| madhivanan,I think I am following you, and yes I think it will work.I will create the rest of the select statements and try it and post back here.Thank you very much!Just because I am curious is there any reason that the case statement gives me syntax errors?Thanks |
 |
|
|
extrapulp
Starting Member
9 Posts |
Posted - 2010-06-15 : 12:46:29
|
madhivanan,I tried your logic and it does not return any results. I started with your code but the stored procedure now returns no results. Here is the exact code I am running.Begin Select PartNum,Min,Max,Available,SalesSample,PendingSale,Notes,SeqNum from dbo.DomeStock Where ( PartNum Like '%'+ @Series + '%' and PartNum not Like '%D' + @Series + '%' and PartNum not Like '%N' + @Series + '%' and PartNum not Like '%C' + @Series + '%' and (@Series = 'F') ) OR ( PartNum Like '%' + @Series + '%' and (@Series = 'DT') ) OR ( PartNum Like '%' + @Series + '%' and PartNum not like '%A%' and PartNum not like '%E%' and PartNum not like '%F%' and PartNum not like '%N%' and (@Series = 'S99') ) Order by PartNum End If I run each select statement that is between the OR I get the results I was expecting.The only way I know to show what I am trying to do is with this example, which will not run due to syntax error 156 near the keyword CASE.CASE @Series When 'F' Then Select PartNum,Min,Max,Available,SalesSample,PendingSale,Notes,SeqNum from dbo.DomeStock Where PartNum Like '%'+ @Series + '%' and PartNum not Like '%D' + @Series + '%' and PartNum not Like '%N' + @Series + '%' and PartNum not Like '%C' + @Series + '%' Order by PartNum When 'S99' Then Select PartNum,Min,Max,Available,SalesSample,PendingSale,Notes,SeqNum from dbo.DomeStock Where PartNum Like '%' + @Series + '%' and PartNum not like '%A%' and PartNum not like '%E%' and PartNum not like '%F%' and PartNum not like '%N%' Order by PartNum End Thanks |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-06-16 : 02:22:01
|
| Can you post some sample data with expected result?MadhivananFailing to plan is Planning to fail |
 |
|
|
extrapulp
Starting Member
9 Posts |
Posted - 2010-06-16 : 14:33:04
|
madhivanan,OK here is some sample data.PartNum Min Max Available SalesSample PendingSaleF10200 0 0 14,230 0F10200N 25000 75000 17,510 0F10200NC 0 0 5,000 1250 per Cart) 0FD10200N 0 0 ,320 0F10260 0 0 19,570 0F10260T 0 0 0 (1000 per Cart) 0U06180 10000 30000 37,220 0U06180N 0 0 13,130 0UD08290 20000 60000 0 0UD08290C 0 0 6,000 (1500 per Cart) 0NS12400N 0 0 2,340 0NS12450 0 0 0P22170 0 0 31,240 0P22220 0 0 9,220 0P22300 0 0 0 0M22170 0 0 3,950 0M22220 0 0 2,940 0C42220 0 0 0C50220 0 0 0RK22170 0 0 4,880 4,880RK35170 0 0 6,420 0A12400 0 0 3,700 0WT04170 5000 20000 25,370 0WTD05200 0 0 12,370 0DT12450N 10,000 30,000 37,470 0DTBL12340N 0 0 4,840 0DTBL12450N 0 0 0 0TB12###N 0 0 0TCF08400 0 0 4,590 0TCG06240 0 0 23,710 0BL08260 0 0 50,000 0BL08400 0 0 2,540 0BL10280 10000 30000 3,890 0GX12700 0 0 32,055 0GX12900 0 0 5,280 0Q08400NT 0 0 11,000 (1000 per Cart) 0Q10200N 0 0 0Q10340NT 0 0 16,000 (1000 per Cart) 0S99366 0 0 5,900 0S99390 50000 75000 40,000 (1,000 per cart) 0S99390C 50000 75000 15,000 0S99394 0 0 -418,330 0E8-TDF-280 0 0 4,260 0E8-TDF-340 0 0 5,830 0E9-RDF-400 0 0 8,285 0E10-RDF-230 0 0 0E10-RDF-280 0 0 0E10-RDF-340 0 0 9,910 0E.5-ONN1-340 3000 10000 2,160 0E.5-ONN1-450 0 0 1,910 0PNP-06 (F06085) 0 0 120 0PNP-06 (F06130) 0 0 0PNP-06 (F06180) 0 0 90 0PNP-06(U06180N) 0 0 90 0 Some example results would be (Im only putting the PartNum as the results):If 'F' is passed F10200F10200NF10200NCFD10200NF10260F10260TIf 'S99' is passed S99366S99390S99390CS99394If 'GX' is passedGX12700GX12900I hope this clears it up some. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-06-17 : 03:04:52
|
Try this Select PartNum,Min,Max,Available,SalesSample,PendingSale,Notes,SeqNum from dbo.DomeStock Where ( PartNum Like @Series + '%' and PartNum not Like '%D' and PartNum not Like '%N' and PartNum not Like '%C' and (@Series = 'F') ) OR ( PartNum Like @Series + '%' and (@Series = 'DT') ) OR ( PartNum Like @Series + '%' and PartNum not like '%A%' and PartNum not like '%E%' and PartNum not like '%F%' and PartNum not like '%N%' and (@Series = 'S99') ) Order by PartNum MadhivananFailing to plan is Planning to fail |
 |
|
|
extrapulp
Starting Member
9 Posts |
Posted - 2010-06-17 : 12:56:42
|
| madhivanan,I was able to add the other select criteria and have everything work.Thank you very much! |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-06-18 : 02:03:11
|
quote: Originally posted by extrapulp madhivanan,I was able to add the other select criteria and have everything work.Thank you very much!
You are welcome MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|