Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering by args in Stored Proc

I have a problem with my code :P obviously right?! Anyway...here's what it is...I want to have a STORED PROC that takes 3 args (@cat, @cutPrice, @option) and returns multiple results that have a type similar to @cat, and a price that is < or > @cutPrice depending on the @option keyword 'above' or 'below'. The problem is when I execute this...

EXEC spBookByPrice  
@cat = 'business', @cutPrice = 0.00, @option = 'above'  

...I get no results, but @option = ' ' shows all prices. Anyway here's my code....

ALTER PROC spBookByPrice
@cat varchar(12), @cutPrice money, @option varchar(5)
AS
BEGIN
    SELECT
    title AS 'Title:',
    type AS 'Category:',
    price AS 'Price:',
    CASE 
        WHEN price >= @cutPrice THEN 'above'
        WHEN price < @cutPrice THEN 'below'
        --ELSE NULL
    END AS 'Option:'
    FROM dbo.titles
    WHERE 'Option:' LIKE '%' + @option + '%'
    GROUP BY type, title, price
    HAVING type LIKE '%' + @cat + '%'
END
like image 542
Kyle Thomas Avatar asked Oct 29 '22 14:10

Kyle Thomas


1 Answers

Is this what you need:

ALTER PROC spBookByPrice
@cat varchar(12), @cutPrice money, @option varchar(5)
AS
BEGIN
    SELECT
    title AS [Title:],
    type AS [Category:],
    price AS [Price:],
    CASE 
        WHEN price >= @cutPrice THEN 'above'
        WHEN price < @cutPrice THEN 'below'
        --ELSE NULL
    END AS [Option:]
    FROM dbo.titles
    WHERE (CASE WHEN price >= @cutPrice THEN 'above'
                WHEN price < @cutPrice THEN 'below'
            --ELSE NULL
            END) LIKE '%' + @option + '%'
    GROUP BY type, title, price
    HAVING type LIKE '%' + @cat + '%'
END
like image 116
Gurwinder Singh Avatar answered Nov 15 '22 05:11

Gurwinder Singh