Solved

Sybase Cursor Problem

Posted on 2004-08-26
3
740 Views
Last Modified: 2008-03-03
Hi
I have a Requirement like this
(if (somecondition)
begin
   DECLARE EMP_CURSOR as select A1,A2 where some condition
end
(if another condition)
begin
   DECLARE EMP_CURSOR as select A1,A2 where some other condition
end
OPEN EMP_CURSOR
/* Do some processing : same for both the condions*/

This is working file if only one condition is satisfied . But if both the conditons are satisfied its giving an error
"there is already a cursor name EMP_CUR at level 1".
I don't want to move the cursor Processing code to both the if condition and duplicate it as it is very big(30 lines) .
Any help will be greatly appricciated..
0
Comment
Question by:moonoo
3 Comments
 
LVL 29

Expert Comment

by:leonstryker
ID: 11902884
Create a variable and set its value based on the first condition being true.  Then test it if the second condition is true:

Select @con1 = 0
(if (somecondition)
begin
   Select @con1 = 1
   DECLARE EMP_CURSOR as select A1,A2 where some condition
end
(if another condition)
   if @con1 = 1
       -- Both conditions are true so the cursor needs to take that into account
   else
       begin
           DECLARE EMP_CURSOR as select A1,A2 where some other condition
       end
OPEN EMP_CURSOR
/* Do some processing : same for both the condions*/

Leon
0
 
LVL 6

Expert Comment

by:ChrisKing
ID: 11913150
could try an "ELSE IF", but you will also need to cater for neither condition being true (you won't want to process the cursor in that case).

if (somecondition)
begin
   DECLARE EMP_CURSOR as select A1,A2 where some condition
end
else if (another condition)
begin
   DECLARE EMP_CURSOR as select A1,A2 where some other condition
end

if (somecondition)
or(another condition)
begin
   OPEN EMP_CURSOR
   /* Do some processing : same for both the condions*/
end
0
 
LVL 19

Accepted Solution

by:
grant300 earned 500 total points
ID: 11947989
The problem is that you have two conditions that are not mutually exclusive.  You actually have to code for three cases:

CASE
  WHEN {somecondition AND anothercondition} THEN
       DECLARE EMP_CURSOR .........where some condition and some other condition
  WHEN {somecondition} THEN
        DECLARE EMP_CURSOR .........where some condition
  WHEN {anothercondition} THEN
       DECLARE EMP_CURSOR ......... where some other condition
END

You can also use IF/ELSE to create unique execution paths for all three cases.

Bill
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 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

14 Experts available now in Live!

Get 1:1 Help Now