0

I got this procedure with a 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

and

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)

I get this error

Must declare the scalar variable "@id"

multiple times, could someone please tell me what I am doing wrong?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Bata
  • 183
  • 2
  • 6
  • 17

1 Answers1

0

At the very least, you need 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;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786