Solved

possible to have Cursor Inside Stored Procedure?

Posted on 2004-03-21
7
461 Views
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...

CREATE PROCEDURE sp_RollingWeekly
       @vStatus varchar(50)

      DECLARE cr_Status CURSOR FOR --ERROR IS HERE
      SELECT Distinct(Status) FROM iTouch WHERE Status is not null
      
      OPEN cr_Status
      FETCH NEXT FROM cr_Status INTO @vStatus
      WHILE @@FETCH_STATUS = 0
            BEGIN
                  FETCH NEXT FROM cr_Status INTO @vStatus
                  Print @vStatus
            END
      CLOSE DKWItems_Cursor
      DEALLOCATE DKWItems_Cursor

GO

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

0
Comment
Question by:jadoger
[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
  • 4
  • 3
7 Comments
 
LVL 34

Accepted Solution

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


CREATE PROCEDURE sp_RollingWeekly
      @vStatus varchar(50)

AS




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

Author Comment

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

Expert Comment

by:arbert
ID: 10645701
Right, but is it something you can accomplish with TSQL NOT using a cursor?
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:jadoger
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...

Status
----------
Send
Failed
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
0
 
LVL 34

Expert Comment

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

Author Comment

by:jadoger
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 :)
0
 
LVL 34

Expert Comment

by:arbert
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....
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

749 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