Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here is my Query, Now I wanted to create a SP with this query. I think I am missing the world's simplest thing in here but not sure of what it is. I have parameters here PRMLOC, PRMICITEM as input parameters.

SQL
SELECT T2 . ICITEM , T2 . BABLOC ,
CHAR ( INTEGER ( ICWDTH ) * 10000 +
INTEGER ( ICASRA ) * 100 + INTEGER ( ICTRIM ) ) TIRE_SIZE
, D . UNITSPER
, E . NBRRCKBINB , F . ICDSC1 , F . ICDSC2 , T2 . TQOH , T2 . TQCM ,
CASE WHEN D . UNITSPER <> 0 THEN
CEIL ( INTEGER ( ( T2 . TQOH ) ) / D . UNITSPER )
ELSE INTEGER ( 0 ) END
NBR_USED_RACKS
, CASE WHEN D . UNITSPER <> 0 THEN
E . NBRRCKBINB - ( CEIL ( INTEGER ( ( T2 . TQOH ) ) / D . UNITSPER ) )
ELSE INTEGER ( 0 ) END
NBR_OPEN_RACKS ,
( E . NBRRCKBINB * D . UNITSPER ) - T2 . TQOH
OPEN_UNITS
FROM (
SELECT A . BACMP , A . BALOC , MIN ( A . BAITEM ) ICITEM
, A . BABLOC , INTEGER ( SUM ( A . BAQOH ) ) TQOH ,
INTEGER ( SUM ( A . BAQCM ) ) TQCM
FROM TESTDATA . VINBINI A WHERE A . BALOC = PRMLOC AND 1 = A . BACMP AND
A . BAQOH - A . BAQCM > 0 GROUP BY A . BACMP , A . BALOC , A . BABLOC HAVING
MIN ( A . BAITEM ) = MAX ( A . BAITEM )
AND SUM ( A . BAQOH - A . BAQCM ) > 0
) AS T2 , TESTDATA . PALITMLOC B , PALBINPF E
, VINITEM F , PALLITEMPF D
WHERE T2 . BACMP = B . TACOMP AND T2 . ICITEM = B . ICITEM
AND T2 . BALOC = B . IALOC AND T2 . ICITEM = F . ICITEM
AND T2 . ICITEM = D . ICITEM
AND B . PALLETID = D . PALLETID
AND E . TACOMP = T2 . BACMP
AND E . IALOC = T2 . BALOC
AND E . IMBLOC = T2 . BABLOC
AND  ICITEM LIKE 'PRMITEM%'
 ORDER BY 1 , 2 ;


Now I want this Query to Run / show results if I pass PRMICTEM as null/ Nothing, it should show all the results. But If I pass PRMICITEM it should only show that particular one like below

ICITEM ICDSC1 BABLOC
1 AB
1 BC
2 AB
3 AB
3 BC
4 DA

Now that should be the result if I pass null/ nothing/empty as PRMICITEM.
If I pass PRMICITEM = '3' then it should only return records with 3


Can you please give me with correct syntax just for this parameter alone?
Posted
Updated 28-Sep-15 6:16am
v2
Comments
Andy Lanng 28-Sep-15 12:25pm    
this query is very poorly formatted. Please add some indents so we can follow your examples

I think you want to get rid of
AND ICITEM LIKE 'PRMITEM%'

and replace it with
AND (ICITEM = @PRMICITEM OR @PRMICITEM IS NULL)
 
Share this answer
 
Comments
sudevsu 28-Sep-15 13:09pm    
Well , yeah something like this. But it didn't fulfill the requirement though. When the parameter is empty it should return all results. If Parameter is '3' then it only returns the records where ICITEM='3'. Meaning, it PRMICITEM is not empty, then it should check for this constraint. otherwise it shouldn't
Wendell D H 28-Sep-15 13:20pm    
Perhaps I still don't under stand...
(ICITEM = @PRMICITEM OR @PRMICITEM IS NULL)
Assuming @PRMICITEM is the parameter...
If @PRMICITEM is three, then it will return all records with ICITEM equal to three. If @PRMICITEM is null (which I am assuming is what is meant by empty) the it will not put any restrictions on ICITEM.
sudevsu 28-Sep-15 13:24pm    
Yeah but it putting restriction on ICITEM if its empty/null. It doesn't return anything. Instead I want it to return all records
Wendell D H 28-Sep-15 13:28pm    
No. See the 'OR'... If @PRMICITEM is null, then it will not put any restrictions on ICITEM. If @PRMICITEM is 3, then it will only get items with ICITEM = 3. Did you try it? Make sure you have the parenthesis correct. (ICITEM = @PRMICITEM OR @PRMICITEM IS NULL)
sudevsu 28-Sep-15 13:42pm    
I did it like below
ICITEM like '@PRMICITEM%'

Because I want a like clause there
Presumably you are building some dynamic SQL due to the way you are using the parameter - if not then this article should help Building Dynamic SQL In a Stored Procedure[^]

The way you are doing your joins is untidy - see Old join syntax vs New[^]

You can utilise the ISNULL[^] in SQL (NULLIF[^] in DB2) e.g.
WHERE ICITEM LIKE ISNULL('PRMITEM%', '%')
 
Share this answer
 
Comments
Maciej Los 29-Sep-15 15:49pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900