我正在使用MySQL创建一个存储过程,该过程将使用各种搜索过滤器进行搜索。我一直在阅读有关如何完成此任务的多项内容,虽然它似乎适用于使用其存储过程的每个人,但我无法弄清楚为什么它在我这样做时不起作用。这是我想要做的:
Create Procedure spSearchInventory
@SKU int(25),
@ItemName varchar(40),
@RetailPrice decimal(7,2),
@WholesalePrice decimal(7,2),
@DateOfPurchase date,
@DateOfPost date
As
Begin
SELECT * FROM inventory WHERE
(SKU = @SKU OR @SKU IS NULL) AND
(ItemName = @ItemName OR @ItemName IS NULL) AND
(RetailPrice = @RetailPrice OR @RetailPrice IS NULL) AND
(WholesalePrice = @WholesalePrice OR @WholesalePrice IS NULL) AND
(DateOfPurchase = @DateOfPurchase OR @DateOfPurchase IS NULL) AND
(DateOfPost = @DateOfPost OR @DateOfPost IS NULL)
END
它给我的错误就在顶部" @SKU int(25)"。它读取的错误是"语法错误:' @ SKU' (在文本后缀处)不是此位置的有效输入。我是SQL的新手,所以这可能是一个简单的问题,但是,这是与其他人使用的完全相同的存储过程,并且取得了成功,所以我不确定为什么它在以后不能工作我试着去做。
答案 0 :(得分:1)
布兰登, 您在过程中使用SQL Server语法。要使用MySQL语法:
create procedure spSearchInventory (
in_SKU int,
in_ItemName varchar(40),
in_RetailPrice decimal(7,2),
in_WholesalePrice decimal(7,2),
in_DateOfPurchase date,
in_DateOfPost date
)
begin
SELECT *
FROM inventory
WHERE
(SKU = in_SKU OR in_SKU IS NULL) AND
(ItemName = in_ItemName OR in_ItemName IS NULL) AND
(RetailPrice = in_RetailPrice OR in_RetailPrice IS NULL) AND
(WholesalePrice = in_WholesalePrice OR in_WholesalePrice IS NULL) AND
(DateOfPurchase = in_DateOfPurchase OR in_DateOfPurchase IS NULL) AND
(DateOfPost = in_DateOfPost OR in_DateOfPost IS NULL);
END