必须使用存储过程声明标量变量“ @id”错误

时间:2018-11-29 02:40:00

标签: sql sql-server stored-procedures database-cursor

我用光标得到了这个过程

CREATE PROCEDURE usp_Inventario
AS
    DECLARE @Id int, @Nombre varchar(255), @precio decimal, @st int, @inv int
    SET @inv = 0

    DECLARE cproducto CURSOR FOR
        SELECT P.ProductID, P.ProductName, P.UnitPrice, P.UnitsInStock
        FROM Products P

OPEN cproducto

FETCH cproducto INTO @id, @Nombre, @precio, @st

WHILE (@@FETCH_STATUS = 0)
BEGIN
    PRINT Str(@id) + space(5) + str(@nombre) + space(5) +  Str(@precio) + space(5) + Str(@st)

    SET @inv += @st

    FETCH cproducto INTO @id, @Nombre, @precio, @st
END

CLOSE cproducto
DEALLOCATE cproducto 

PRINT 'Inventario de Productos:' + Str(@inv)

我收到此错误

  

必须声明标量变量“ @id”

多次,有人可以告诉我我在做什么错吗?

1 个答案:

答案 0 :(得分:0)

至少,您需要begin / end

CREATE PROCEDURE usp_Inventario
AS
BEGIN
    DECLARE @Id int, @Nombre varchar(255), @precio decimal, @st int, @inv int;
    SET @inv=0;
    DECLARE cproducto CURSOR FOR
    SELECT  P.ProductID, P.ProductName, P.UnitPrice, P.UnitsInStock
    FROM Products P;

    . . .
END;
相关问题