?
Solved

How to create Cursor Variable

Posted on 2008-10-13
6
Medium Priority
?
1,866 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
6 Comments
 
LVL 17

Accepted Solution

by:
HuyBD earned 2000 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

Question has a verified solution.

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

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

770 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