【问题标题】:How to assign values to variables in SQL stored procedure?如何为 SQL 存储过程中的变量赋值?
【发布时间】:2017-10-06 11:50:31
【问题描述】:

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

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

【问题讨论】:

  • 标记您正在使用的 dbms。该代码是特定于产品的。
  • 您的示例不是“SQL”,它是 SQL 查询语言的某种程序扩展。
  • 您的脚本似乎做了三件事,将变量声明为 INTEGER,从表中选择名称与我假设在存储过程调用时声明的变量匹配的所有行(注意这些不会去任何地方),将变量的值设置为表中的每个数量。我认为您需要将第 2 步和第 3 步结合在一起吗?

标签: sql variables select stored-procedures declare


【解决方案1】:

您将其声明为 int。您的查询可能返回多个值,因此结果不能存储在此变量中。将您的查询限制为仅返回一个值或将您的变量声明为 int 表。

【讨论】:

    【解决方案2】:

    我认为这就是你想要做的。

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

    【讨论】:

      【解决方案3】:

      也许是这样:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多