0

如何修改以下语句以从代码中删除硬编码值 $1.00 和 $2.00 并将它们放在常量表中?

桌子sec0306_price_constants已经摆好了。min_price字段包含 1.00,字段max_price包含 2.00。

  SELECT description
    FROM l_foods
   WHERE price BETWEEN 1.00 AND 2.00
ORDER BY description

你能向我解释一下这样做的方法吗?

这是给甲骨文的。

4

4 回答 4

3

也许是这样的:

SELECT description
FROM l_foods
WHERE (price >= (select min_price from sec0306_price_constants )
AND price <= (select max_price from sec0306_price_constants ))
ORDER BY description
于 2012-09-26T21:54:32.160 回答
1
declare @constants table (typ varchar(10), minprice float, maxprice float)

insert into @constants values 
('dirtcheap', 0.00, 0.99),
('justright', 1.00, 2.00),
('expensive', 2.01, 10.00)

select description
from   l_foods f, @constants c
where  f.price >= c.minprice and f.price <= c.maxprice
  and  c.typ = 'justright'

您可以通过不同类型的价格范围来限定,而无需更改查询中的低值和高值——只需更改常量类型。

于 2012-09-26T21:58:36.657 回答
0

抱歉,阅读了额外的信息,如果您有表格设置,我会按照以下方式组织内容:

--BEGIN PROC

DECLARE @MINPRICE DECIMAL;
DECLARE @MAXPRICE DECIMAL;

SELECT
    @MINPRICE = min_price,
    @MAXPRICE = max_price
FROM
    sec0306_price_constants;

SELECT description
    FROM l_foods
WHERE 
    price BETWEEN @MINPRICE AND @MAXPRICE
ORDER BY description
于 2012-09-26T21:55:15.490 回答
0
SELECT Description, price, min_price, max_price

FROM l_foods,sec0306_price_constants

WHERE (price >= (sec0306_price_constants.min_price ) AND price <= (sec0306_price_constants.max_price ))

ORDER BY description;
于 2020-09-12T17:30:21.690 回答