Must declare the scalar variable “@id” error with stored procedure





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







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?










share|improve this question

























  • please specify what RDBMS are you using.

    – 1010
    Nov 29 '18 at 2:41











  • SQL SERVER 2017 using NORTHWND database

    – Bata
    Nov 29 '18 at 2:42






  • 2





    Is your database collation case sensitive? You declared @Id with uppercase but use @id.

    – 1010
    Nov 29 '18 at 2:52











  • It's not case sensitive

    – Bata
    Nov 29 '18 at 2:57






  • 1





    The query you've pasted would not produce that error (so long as the top part and the bottom part are in the same scope)...

    – ZLK
    Nov 29 '18 at 3:16


















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?










share|improve this question

























  • please specify what RDBMS are you using.

    – 1010
    Nov 29 '18 at 2:41











  • SQL SERVER 2017 using NORTHWND database

    – Bata
    Nov 29 '18 at 2:42






  • 2





    Is your database collation case sensitive? You declared @Id with uppercase but use @id.

    – 1010
    Nov 29 '18 at 2:52











  • It's not case sensitive

    – Bata
    Nov 29 '18 at 2:57






  • 1





    The query you've pasted would not produce that error (so long as the top part and the bottom part are in the same scope)...

    – ZLK
    Nov 29 '18 at 3:16














0












0








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?










share|improve this question
















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?







sql sql-server stored-procedures database-cursor






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 29 '18 at 5:16









marc_s

584k13011241270




584k13011241270










asked Nov 29 '18 at 2:39









BataBata

165




165













  • please specify what RDBMS are you using.

    – 1010
    Nov 29 '18 at 2:41











  • SQL SERVER 2017 using NORTHWND database

    – Bata
    Nov 29 '18 at 2:42






  • 2





    Is your database collation case sensitive? You declared @Id with uppercase but use @id.

    – 1010
    Nov 29 '18 at 2:52











  • It's not case sensitive

    – Bata
    Nov 29 '18 at 2:57






  • 1





    The query you've pasted would not produce that error (so long as the top part and the bottom part are in the same scope)...

    – ZLK
    Nov 29 '18 at 3:16



















  • please specify what RDBMS are you using.

    – 1010
    Nov 29 '18 at 2:41











  • SQL SERVER 2017 using NORTHWND database

    – Bata
    Nov 29 '18 at 2:42






  • 2





    Is your database collation case sensitive? You declared @Id with uppercase but use @id.

    – 1010
    Nov 29 '18 at 2:52











  • It's not case sensitive

    – Bata
    Nov 29 '18 at 2:57






  • 1





    The query you've pasted would not produce that error (so long as the top part and the bottom part are in the same scope)...

    – ZLK
    Nov 29 '18 at 3:16

















please specify what RDBMS are you using.

– 1010
Nov 29 '18 at 2:41





please specify what RDBMS are you using.

– 1010
Nov 29 '18 at 2:41













SQL SERVER 2017 using NORTHWND database

– Bata
Nov 29 '18 at 2:42





SQL SERVER 2017 using NORTHWND database

– Bata
Nov 29 '18 at 2:42




2




2





Is your database collation case sensitive? You declared @Id with uppercase but use @id.

– 1010
Nov 29 '18 at 2:52





Is your database collation case sensitive? You declared @Id with uppercase but use @id.

– 1010
Nov 29 '18 at 2:52













It's not case sensitive

– Bata
Nov 29 '18 at 2:57





It's not case sensitive

– Bata
Nov 29 '18 at 2:57




1




1





The query you've pasted would not produce that error (so long as the top part and the bottom part are in the same scope)...

– ZLK
Nov 29 '18 at 3:16





The query you've pasted would not produce that error (so long as the top part and the bottom part are in the same scope)...

– ZLK
Nov 29 '18 at 3:16












1 Answer
1






active

oldest

votes


















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;





share|improve this answer
























  • It still says: Must declare the scalar variable "@id" :(

    – Bata
    Nov 29 '18 at 2:59











  • @Bata . . . @id is declared in the stored procedure. Is all your code between the begin and end?

    – Gordon Linoff
    Nov 29 '18 at 13:14











  • Yes that's all of it

    – Bata
    Nov 30 '18 at 1:06












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53531050%2fmust-declare-the-scalar-variable-id-error-with-stored-procedure%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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;





share|improve this answer
























  • It still says: Must declare the scalar variable "@id" :(

    – Bata
    Nov 29 '18 at 2:59











  • @Bata . . . @id is declared in the stored procedure. Is all your code between the begin and end?

    – Gordon Linoff
    Nov 29 '18 at 13:14











  • Yes that's all of it

    – Bata
    Nov 30 '18 at 1:06
















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;





share|improve this answer
























  • It still says: Must declare the scalar variable "@id" :(

    – Bata
    Nov 29 '18 at 2:59











  • @Bata . . . @id is declared in the stored procedure. Is all your code between the begin and end?

    – Gordon Linoff
    Nov 29 '18 at 13:14











  • Yes that's all of it

    – Bata
    Nov 30 '18 at 1:06














0












0








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;





share|improve this answer













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;






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 29 '18 at 2:44









Gordon LinoffGordon Linoff

794k37318421




794k37318421













  • It still says: Must declare the scalar variable "@id" :(

    – Bata
    Nov 29 '18 at 2:59











  • @Bata . . . @id is declared in the stored procedure. Is all your code between the begin and end?

    – Gordon Linoff
    Nov 29 '18 at 13:14











  • Yes that's all of it

    – Bata
    Nov 30 '18 at 1:06



















  • It still says: Must declare the scalar variable "@id" :(

    – Bata
    Nov 29 '18 at 2:59











  • @Bata . . . @id is declared in the stored procedure. Is all your code between the begin and end?

    – Gordon Linoff
    Nov 29 '18 at 13:14











  • Yes that's all of it

    – Bata
    Nov 30 '18 at 1:06

















It still says: Must declare the scalar variable "@id" :(

– Bata
Nov 29 '18 at 2:59





It still says: Must declare the scalar variable "@id" :(

– Bata
Nov 29 '18 at 2:59













@Bata . . . @id is declared in the stored procedure. Is all your code between the begin and end?

– Gordon Linoff
Nov 29 '18 at 13:14





@Bata . . . @id is declared in the stored procedure. Is all your code between the begin and end?

– Gordon Linoff
Nov 29 '18 at 13:14













Yes that's all of it

– Bata
Nov 30 '18 at 1:06





Yes that's all of it

– Bata
Nov 30 '18 at 1:06




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53531050%2fmust-declare-the-scalar-variable-id-error-with-stored-procedure%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Lallio

Futebolista

Jornalista