Solved

Sybase Cursor Problem

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

By reading this blog, MSPs will gain insight into how to improve communications with their clients as well as establish a more profitable business.
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

710 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