如何给 SQL 存储过程中的变量赋值?

How to assign values to variables in SQL stored procedure?

我正在尝试创建一个变量来指示库存中剩余的产品数量,但在 visual studio 中它提醒正在为 varialvel @quantityProduct 分配多个值,有人可以帮助我吗?

BEGIN
Declare @quantityProduct int
Select * from tbStore
Where name = @product
Set @quantityProduct = (select (quantity) from tbStore)
END

也许是这样:

Declare @quantityProduct int
Select @quantityProduct = quantity 
From tbStore
Where name = @product

我想这就是你想要做的。

BEGIN
Declare @quantityProduct int
Select @quantityProduct = quantity
from tbStore
Where name = @product
END

您将其声明为 int。您的查询可能 return 多个值,因此结果无法存储在该变量中。将您的查询限制为 return 只有一个值或将您的变量声明为 table 的 int.