Solved

How to create Cursor Variable

Posted on 2008-10-13
6
1,758 Views
Last Modified: 2012-05-05
I have create the following cursor and am attempting to run via MS Studio Express. When execute I get the following error but not sure how to correct:  Msg 16948, Level 16, State 4, Line 40
The variable '@MasterMenuNumber' is not a cursor variable, but it is used in a place where a cursor variable is expected.



DECLARE @MasterMenuNumber Int;
 
DECLARE MyCursor CURSOR LOCAL
FAST_FORWARD
FOR
SELECT MasterMenuNumber
FROM F01.MstrMenu
 
OPEN MyCursor
 
FETCH NEXT FROM MyCursor
INTO @MasterMenuNumber
 
WHILE @@FETCH_STATUS = 0
BEGIN
 
DELETE FROM F01.BamRecs FROM F01.BldAMenu
   WHERE BamRecs.BAMenuNumber=BldAMenu.BAMenuNumber AND BldAMenu.MasterMenuNumber= @MasterMenuNumber
      AND BldAMenu.DietNumber not in
   (SELECT DietNumber from F01.MMDiets where MasterMenuNumber=@MasterMenuNumber);
 
DELETE FROM F01.BldAMenu WHERE MasterMenuNumber= @MasterMenuNumber 
  AND BAMenuNumber not in (SELECT BAMenuNumber FROM F01.BamRecs);
 
DELETE FROM F01.Snacks FROM F01.SnackRec
   WHERE SnackRec.SNAMenuNumber=Snacks.SNAMenuNumber AND Snacks.MasterMenuNumber= @MasterMenuNumber
      AND Snacks.DietNumber not in
   (SELECT DietNumber from F01.MMDiets where MasterMenuNumber=@MasterMenuNumber);
 
DELETE FROM F01.Snacks WHERE MasterMenuNumber= @MasterMenuNumber 
  AND SNAMenuNumber not in (SELECT SNAMenuNumber FROM F01.SnackRec);
 
FETCH NEXT FROM MyCursor
INTO @MasterMenuNumber
 
END
 
CLOSE MyCursor
DEALLOCATE MyCursor
DEALLOCATE @MasterMenuNumber

Open in new window

0
Comment
Question by:JoeSnyderJr
  • 5
6 Comments
 
LVL 17

Accepted Solution

by:
HuyBD earned 500 total points
ID: 22708282
this cause by line 40
DEALLOCATE @MasterMenuNumber
you dont need to delocate
0
 
LVL 17

Expert Comment

by:HuyBD
ID: 22708289
0
 
LVL 17

Expert Comment

by:HuyBD
ID: 22708301
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 17

Expert Comment

by:HuyBD
ID: 22708307
sorry, I have a missing, wrong window :)
0
 

Author Closing Comment

by:JoeSnyderJr
ID: 31505756
Thanks, you even anticipated my DeAllocate question for the @variable. Thanks for quick response
Were you suggesting by your additional link that a case statement would be alternative to my using a cursor in this situation??
0
 
LVL 17

Expert Comment

by:HuyBD
ID: 22708359
that is wrong post, but you can change to use query to delete
delete yourtable
from yourtable
inner join ....
where ....
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Upgrading SQL 2005 Express to 2008 R2 Express 31 130
get most recent and second most recent date in SQL Server 24 98
Sql query 107 73
Help  needed 3 36
This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question