2016-12-31 115 views
5

我的代碼有問題:P顯然是正確的?!無論如何...這是它是什麼...我想有一個STORED PROC需要3個參數(@cat,@cutPrice,@option)並返回多個類似於@cat的結果,價格是<或> @cutPrice,具體取決於@option關鍵字'上方'或'下方'。問題是當我執行這個...在存儲過程中通過args進行篩選

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

...我沒有得到結果,但@option =''顯示所有價格。反正這裏是我的代碼....

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 

回答

2

這是你所需要的:

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

謝謝Gurwinder,正是我所缺少!!!! –

+0

我只需要用完整的CASE語句 –

+0

@Kyle Exactly替換[Option:] alias .. :) – 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))