The documentation above says:
@cursor_variable_name
Is the name of a cursor variable. Cursor variable names must begin with an at (@) sign and conform to the rules for identifiers.
When i am using the following SQL-Command i get a syntax error and the code to create a stored procedure cannot be executed:
DECLARE @myCursor CURSOR FOR
SELECT Id FROM t_test;
The following code is working without any issues:
CREATE PROCEDURE proc_test
AS
BEGIN
SET NOCOUNT ON;
-- No @-sign used here
DECLARE myCursor CURSOR FOR
SELECT Id FROM t_test;
-- Code of the stored procedure. Omitted for clarification
END
I assume there is a error in the documentation? I tested it with a SQL-Server 2016 Express (local installed) and 2019-latest (docker image). Both with the same result, that a cursor-name starting with 'at-sign' is not working.
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
@mbedded -- Matthias, thank you for your feedback.
@rothja -- Jason, please look into this issue #4124.
@mbedded Thanks for this good question. You are right that in your example, you do not need the @ symbol and, in fact, you get an error. The way you are using cursors is actually described in this article on DECLARE CURSOR. The article you are referring to here is about declaring a cursor variable instead, which is slightly different. It would look something like this:
DECLARE @myCursor CURSOR;
SET @myCursor = CURSOR FOR
SELECT ...;
OPEN @myCursor;
We don't show an example like this in the article, so I can see why it would be confusing. But the way you are using cursors works well and is perfectly valid. I apologize for the confusion. At this time, I am going to close this issue as resolved, but please let me know if you have any other questions! Thank you!
@rothja Thank you very much for your explanation. Do you think it would make sense to point this difference out with one sentence to make it clear for other people as well?
On the other hand: It seems noone (except me) ran into this confusin :laughing:
@mbedded I'm sure that you're not the only one. And, yes, a sentence to this effect might be helpful. I'll try to add that soon. Or, if you'd like, you can edit the topic, add the sentence where you think it makes sense, and submit it to us. If you want to contribute to the article, let me know. Otherwise, I'll try to do this within a week or two as I have time. Thank you!
To be honest i am unsure how to phrase it specific enough so everybody can understand it. I needed a moment to understand the difference but i can't point it out specific enough. I see a different syntax but maybe i miss some background information. For me, we use DECLARE in both cases. In one of them its directly combined with "cursor for" and the other one does it separately.
So i would appreciate if you could handle this task. I think it should be added to the middle of the document. There is already a section called Remarks with A cursor variable so we could add it there.
@mbedded Thanks. I'll try to work on adding this soon.