我的代码有问题:P显然对吗?!无论如何...这里是什么...我想要一个STORED PROC,它需要3个args(@ cat,@ cutPrice,@ option)并返回多个类似于@cat的结果,并且价格低于<或者> @cutPrice取决于@option关键字'以上'或者'以下'问题是我执行这个......
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
答案 0 :(得分: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
答案 1 :(得分: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))