2016-12-31 3 views
5

Ich habe ein Problem mit meinem Code: P offensichtlich richtig ?! Wie auch immer ... hier ist, was es ist ... Ich möchte einen STORED PROC haben, der 3 Argumente (@cat, @cutPrice, @option) hat und mehrere Ergebnisse zurückgibt, die einen ähnlichen Typ wie @cat haben, und einen Preis, der ist < oder> @cutPrice abhängig vom @option-Schlüsselwort "über" oder "unter". Das Problem ist, wenn ich diese ...Filterung durch Argumente in Stored Proc

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

... ich bekomme keine Ergebnisse, aber @option = '' zeigt alle Preise. Auf jeden Fall ist hier mein 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 

Antwort

2

das ist, was Sie brauchen:

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 
+0

Sie Gurwinder Danke, genau das was ich fehlte !!!! –

+0

Ich musste nur den [Option:] Alias ​​mit der vollständigen CASE-Anweisung ersetzen –

+0

@Kyle Genau .. :) – GurV

0
select title [Title:], type [Category:], price [Price:], 
    [Option:] = iif(price >= @cutPrice, 'above', 'below') 
from titles 
where type like concat('%', @cat, '%') and (@option = '' 
    or (@option='below' and price < @cutPrice) 
    or (@option='above' and price >= @cutPrice)) 
Verwandte Themen