possible to have Cursor Inside Stored Procedure?

Posted on 2004-03-21
Last Modified: 2008-03-03
Hi All
I am trying to create few cursors inside a stored procedure which will be used for reporting from crystal reports, adn receiving the following error

Server: Msg 156, Level 15, State 1, Procedure sp_RollingWeekly, Line 4
Incorrect syntax near the keyword 'DECLARE'.

Stored procedure is...

       @vStatus varchar(50)

      SELECT Distinct(Status) FROM iTouch WHERE Status is not null
      OPEN cr_Status
      FETCH NEXT FROM cr_Status INTO @vStatus
                  FETCH NEXT FROM cr_Status INTO @vStatus
                  Print @vStatus
      CLOSE DKWItems_Cursor
      DEALLOCATE DKWItems_Cursor


can any one tell me that whether cursors can be used inside a stored procedure, if yes then what is the correct syantax?
Many thanks

Question by:jadoger
  • 4
  • 3
LVL 34

Accepted Solution

arbert earned 50 total points
ID: 10645664
You're missing the keyword AS

      @vStatus varchar(50)


What's the end result--cursors are usually pretty slow and should be avoided...

Author Comment

ID: 10645691
oopps thanks
I have to create this stored procedure and make some calcualations which are not possible through crystal report
LVL 34

Expert Comment

ID: 10645701
Right, but is it something you can accomplish with TSQL NOT using a cursor?
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.


Author Comment

ID: 10645715
dont no, Actully I just want to loop through a look-up table nad then want to ahve certain calculations based on each row of that lookup table
Loopup table is...

Not Send

now i want to loop through that table and calculate that how many values aggainst each of this status is present in some other table, is there any other way of doin this apart from cursor ? i would love no that
LVL 34

Expert Comment

ID: 10645726
Ya, a simple join between the tables and do your calculation.....

Author Comment

ID: 10645744
:) ya  i wish i could do that but my client is not giving me access to the database so all what I have is the dummy one table created in my database in which there is a field named STATUS so i am trying to first select all the possible distinct values from that table and then procced calculations, still dont no that whether i would have direct access to client database or not, so in any case i want to complete my assignment so i can give him report based on the data he gave me, yeah i can create a lookup table in my database and can use Join but not sure that whether the lookup table is in client database or not, the file which he has provided is an output of a stored procedure. thats the story mate :)
LVL 34

Expert Comment

ID: 10645939
I guess I'm confused.  You don't have to have a lookup table--if you have a status field, you just perform calculation on the "status" field using a case statement--no lookup table needed (though it would be easier).  Still much less work than a cursor....

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Slow Connectivity over ODBC 8 35
Refresh Dev server with Production database 8 28
Help Required 3 96
SQL Count issue 24 15
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

773 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