Solved

Sybase Cursor Problem

Posted on 2004-08-26
3
764 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

An article on effective troubleshooting
Google always has something new and amazing up its sleeve, and the most current thing that they have been working on is another step in the evolution of Google Search, from machine learning to its brilliant successor, deep learning.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

803 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