为什么在函数中使用运算符对我不起作用?
IF OBJECT_ID('Sales.fn_FilteredExtension','IF') IS NOT NULL DROP FUNCTION Sales.fn_FilteredExtension;
GO
CREATE FUNCTION Sales.fn_FilteredExtension
(
@lowqty smallint,
@highqty smallint
)
RETURNS TABLE AS RETURN
(
SELECT unitprice,qty
FROM Sales.OrderDetails where (qty <= @highqty) AND (qty >= @lowqty)
);
GO
我跑步时
SELECT * FROM Sales.fn_FilteredExtension(20,15)
我得到空白结果,但是当我使用
IF OBJECT_ID('Sales.fn_FilteredExtension','IF') IS NOT NULL DROP FUNCTION Sales.fn_FilteredExtension;
GO
CREATE FUNCTION Sales.fn_FilteredExtension
(
@lowqty smallint,
@highqty smallint
)
RETURNS TABLE AS RETURN
(
SELECT unitprice,qty
FROM Sales.OrderDetails where qty BETWEEN @highqty AND @lowqty
);
GO
我得到输出。有什么区别?
答案 0 :(得分:2)
您需要传递正确的参数:
SELECT * FROM Sales.fn_FilteredExtension(20,15)
<=>
SELECT * FROM Sales.fn_FilteredExtension(15,20)
答案 1 :(得分:1)
我认为您正在混合参数,然后颠倒 您的BETWEEN语句将最小值作为第一个参数,将较高的值作为第二个参数,因此,如果@lowqty实际上收到最小值,则它不起作用
更改“ FROM Sales.OrderDetails,其中@highqty和@lowqty之间的数量”
转到“从Sales.OrderDetails到@lowqty和@highqty之间的数量”
然后确保@lowqty实际上是传递的最小值
考虑到单词BETWEEN在通常用法中的含义,然后再输入错误的值,容易混淆