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;
}
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
|
show 4 more comments
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
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@Idwith 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
|
show 4 more comments
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
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
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@Idwith 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
|
show 4 more comments
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@Idwith 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
|
show 4 more comments
1 Answer
1
active
oldest
votes
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;
It still says: Must declare the scalar variable "@id" :(
– Bata
Nov 29 '18 at 2:59
@Bata . . .@idis declared in the stored procedure. Is all your code between thebeginandend?
– Gordon Linoff
Nov 29 '18 at 13:14
Yes that's all of it
– Bata
Nov 30 '18 at 1:06
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
It still says: Must declare the scalar variable "@id" :(
– Bata
Nov 29 '18 at 2:59
@Bata . . .@idis declared in the stored procedure. Is all your code between thebeginandend?
– Gordon Linoff
Nov 29 '18 at 13:14
Yes that's all of it
– Bata
Nov 30 '18 at 1:06
add a comment |
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;
It still says: Must declare the scalar variable "@id" :(
– Bata
Nov 29 '18 at 2:59
@Bata . . .@idis declared in the stored procedure. Is all your code between thebeginandend?
– Gordon Linoff
Nov 29 '18 at 13:14
Yes that's all of it
– Bata
Nov 30 '18 at 1:06
add a comment |
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;
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;
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 . . .@idis declared in the stored procedure. Is all your code between thebeginandend?
– Gordon Linoff
Nov 29 '18 at 13:14
Yes that's all of it
– Bata
Nov 30 '18 at 1:06
add a comment |
It still says: Must declare the scalar variable "@id" :(
– Bata
Nov 29 '18 at 2:59
@Bata . . .@idis declared in the stored procedure. Is all your code between thebeginandend?
– 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
@Idwith 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