Solved

How to create Cursor Variable

Posted on 2008-10-13
6
1,712 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server Degrading on Write 13 63
ms sql + top 1 for each customer 3 43
CREATE DATABASE ENCRYPTION KEY 1 56
create insert script based on records in a table 4 11
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
A short film showing how OnPage and Connectwise integration works.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

943 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

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now