Question : Mysql Stored proc pass all values help

Hi guys.

I have attached a stored procedure.
For store, brand, depart, subd, product, categ fabric, season and purchase parameters, If i want ALL VALUES,
Is there an easier faster way then, using REGEXP and passing it eg '[-0-9]'  in the call for everything.

is there a way to just NOT include the parameter, so everything is passed.

Like if BRAND = '' then pass it everything..

Hope that makes sense..
Kj
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
DELIMITER $$
DROP PROCEDURE IF EXISTS `BW` $$
CREATE PROCEDURE `BW`(IN STORE VARCHAR(2555),
                      IN BRAND VARCHAR(2555),
                      IN DEPART VARCHAR(2555),
                      IN SUBD VARCHAR(2555),
                      IN PRODUCT VARCHAR(2555),
                      IN CATEG VARCHAR(2555),
                      IN FABRIC VARCHAR(2555),
                      IN SEASON VARCHAR(2000),
                      IN PURCHASE VARCHAR(2000),
                      IN STYLE VARCHAR(255),
                      IN STARTDATE VARCHAR(255),
                      IN ENDDATE VARCHAR(255))

BEGIN

    SET @bws = CONCAT("

Select s.storename,
       s.storenumber,
       s.brandname,
       s.brandnumber,
       s.deptname,
       s.deptcode,
       s.subdname,
       s.subdcode,
       s.prodname,
       s.prodcode,
       s.catename,
       s.catecode,
       s.fabricname,
       s.fabricno,
       s.seasname,
       s.seascode,
       s.purchflagdescription,
       s.purchflagcode,
       s.stylecode,
       s.description,
       s.datefirstrecv,
       s.datelastrecv,
       sum(h.quantity) as SOLD,
       s.datelastsold,


(select ifnull(sum(h.quantity),0)
from history h
where (h.stockcode = s.stockcode
and h.storenumber = s.storenumber)
and h.subtranstype = 6
and s.storenumber IN   (",STORE,")
and s.brandnumber IN   (",BRAND,")
and s.deptcode REGEXP    ",DEPART,"
and s.subdcode REGEXP    ",SUBD,"
and s.prodcode REGEXP    ",PRODUCT,"
and s.catecode REGEXP    ",CATEG,"
and s.fabricno REGEXP    ",FABRIC,"
and s.seascode REGEXP    ",SEASON,"
and s.purchflagcode REGEXP   ",PURCHASE,"
and s.stylecode LIKE     ",STYLE,"
and (s.datefirstrecv or s.datelastrecv or s.datelastsold) is not null
and  h.transdate between ",STARTDATE," and ",ENDDATE," ) as QTYRECV,


(select ifnull(sum(h.quantity),0)
from history h
where (h.stockcode = s.stockcode
and h.storenumber = s.storenumber)
and h.subtranstype < '4'
and s.storenumber IN   (",STORE,")
and s.brandnumber IN   (",BRAND,")
and s.deptcode REGEXP    ",DEPART,"
and s.subdcode REGEXP    ",SUBD,"
and s.prodcode REGEXP    ",PRODUCT,"
and s.catecode REGEXP    ",CATEG,"
and s.fabricno REGEXP    ",FABRIC,"
and s.seascode REGEXP    ",SEASON,"
and s.purchflagcode REGEXP   ",PURCHASE,"
and s.stylecode LIKE     ",STYLE,"
and (s.datefirstrecv or s.datelastrecv or s.datelastsold) is not null
and  h.transdate between ",STARTDATE," and ",ENDDATE," ) ONHAND




from history h INNER JOIN styleperformance s
ON (h.storenumber=s.storenumber
and h.stockcode=s.stockcode)
and h.subtranstype < '4'
and s.storenumber IN   (",STORE,")
and s.brandnumber IN   (",BRAND,")
and s.deptcode RLIKE    ",DEPART,"
and s.subdcode RLIKE    ",SUBD,"
and s.prodcode RLIKE    ",PRODUCT,"
and s.catecode RLIKE    ",CATEG,"
and s.fabricno RLIKE    ",FABRIC,"
and s.seascode RLIKE    ",SEASON,"
and s.purchflagcode RLIKE   ",PURCHASE,"
and s.stylecode LIKE     ",STYLE,"
and (s.datefirstrecv or s.datelastrecv or s.datelastsold) is not null
and  h.transdate between ",STARTDATE," and ",ENDDATE,"
group by s.storenumber, s.stockcode
ORDER BY SOLD DESC
LIMIT 20  ");

#SELECT @bws;
PREPARE b_w_s from @bws;
EXECUTE b_w_s;
DEALLOCATE PREPARE b_w_s;

END $$
DELIMITER;

CALL `BW`("1,2,9,10","1,2,3,4,5,6,7,8,9,10,11,12,13,14,15","'[-0-9]'","'[-0-9]'","'[-0-9]'","'[-0-9]'","'[-0-9a-z]'","'[-0-9a-z]'","'[-0-9a-z]'","'%'","'2010-06-01'","'2010-07-03'")

Answer : Mysql Stored proc pass all values help

and s.storenumber IN   (",STORE,") ",
     CASE WHEN BRAND="" then "" else concat("and s.brandnumber IN   (",BRAND,")") END,
" and s.deptcode REGEXP    ",DEPART,"

Okay I missed an END.
Basically, what it means is, at the part where you normally add the string

and s.brandnumber IN   (",BRAND,")

To your  full SQL, it checks for whether Brand is "".  If it is blank, it adds nothing to the final query.  Looking at just the CASE line, it does this:

     CASE WHEN BRAND="" then
         ""
     else
         concat("and s.brandnumber IN   (",BRAND,")")
     END,

So it either adds "and s.brandnumber in (<brand>)" if the brand filter is not empty, or nothing at all (and therefore the entire sql does not filter on brand).

Let me know if this is unclear.
Random Solutions  
 
programming4us programming4us