473,396 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to select a WHERE condition depending on another cond

16
Hi all,

Can u pls help me on this.

I want to have something like below in procedure.

----------------------------------
FOR CUR_A IN (
SELECT A.AMOUNT , B.PLANID ,B.EVENTCD
FROM ACCUMULATON A, PLAN B
WHERE A.PLANID = B.PLANID
AND B.DEALERID = 1
IF aFlag = 1 THEN
AND A.OFFERING = B.OFFERING
ENDIF
)
LOOP ......
--------------------

The if else statement is what I want given logically. aFlag is a variable to be set dynamically. I want the last AND condition to be used depending on the Flag value. The Cursor Loop must stay.

Can any one tell me how can do this?

saby
Nov 22 '06 #1
6 3335
willakawill
1,646 1GB
Hi all,

Can u pls help me on this.

I want to have something like below in procedure.

----------------------------------
FOR CUR_A IN (
SELECT A.AMOUNT , B.PLANID ,B.EVENTCD
FROM ACCUMULATON A, PLAN B
WHERE A.PLANID = B.PLANID
AND B.DEALERID = 1
IF aFlag = 1 THEN
AND A.OFFERING = B.OFFERING
ENDIF
)
LOOP ......
--------------------

The if else statement is what I want given logically. aFlag is a variable to be set dynamically. I want the last AND condition to be used depending on the Flag value. The Cursor Loop must stay.

Can any one tell me how can do this?

saby
Try this out:
Expand|Select|Wrap|Line Numbers
  1. FOR CUR_A IN (
  2. IF aFlag = 1 THEN
  3. SELECT A.AMOUNT , B.PLANID ,B.EVENTCD
  4. FROM ACCUMULATON A, PLAN B
  5. WHERE A.PLANID = B.PLANID
  6. AND B.DEALERID = 1
  7. AND A.OFFERING = B.OFFERING
  8. ELSE
  9. SELECT A.AMOUNT , B.PLANID ,B.EVENTCD
  10. FROM ACCUMULATON A, PLAN B
  11. WHERE A.PLANID = B.PLANID
  12. AND B.DEALERID = 1
  13. ENDIF
  14. )
  15. LOOP ......
  16.  
  17.  
Nov 22 '06 #2
Hi all,

Can u pls help me on this.

I want to have something like below in procedure.

----------------------------------
FOR CUR_A IN (
SELECT A.AMOUNT , B.PLANID ,B.EVENTCD
FROM ACCUMULATON A, PLAN B
WHERE A.PLANID = B.PLANID
AND B.DEALERID = 1
IF aFlag = 1 THEN
AND A.OFFERING = B.OFFERING
ENDIF
)
LOOP ......
--------------------

The if else statement is what I want given logically. aFlag is a variable to be set dynamically. I want the last AND condition to be used depending on the Flag value. The Cursor Loop must stay.

Can any one tell me how can do this?

saby
The first idea I have is to do somthing like this:

DECLARE

CURSOR CUR_A(aFlag IN NUMBER)
IS
SELECT A.AMOUNT , B.PLANID ,B.EVENTCD
FROM ACCUMULATON A, PLAN B
WHERE A.PLANID = B.PLANID
AND B.DEALERID = 1
AND aFlag <> 1

UNION

SELECT A.AMOUNT , B.PLANID ,B.EVENTCD
FROM ACCUMULATON A, PLAN B
WHERE A.PLANID = B.PLANID
AND B.DEALERID = 1
AND aFlag = 1
AND A.OFFERING = B.OFFERING;

flag NUMBER;

BEGIN

flag := 1;

FOR CUR_A_REC IN CUR_A(flag) LOOP

.......................................
.......................................

END LOOP;

END;
Nov 22 '06 #3
pragatiswain
96 Expert
I feel willakawill's solution is acceptable as per the question.
Nov 22 '06 #4
saby
16
Thanks.
Sorry, but IF --ELSE is not being accepted within FOR -- IN.
The following error coming up while compiling.
---------------------------------------------------------------------------
ORA-06550: line 7, column 4:
PLS-00103: Encountered the symbol "IF" when expecting one of the following:

( - + case mod new not null others select <an identifier>
<a double-quoted delimited-identifier> <a bind variable> avg
count current exists max min prior sql stddev sum variance
execute forall merge time timestamp interval date
<a string literal with character set specification>
<a number> <a single-quoted SQL string> pipe
--------------------------------------------------------------------------------

saby

Try this out:
Expand|Select|Wrap|Line Numbers
  1. FOR CUR_A IN (
  2. IF aFlag = 1 THEN
  3. SELECT A.AMOUNT , B.PLANID ,B.EVENTCD
  4. FROM ACCUMULATON A, PLAN B
  5. WHERE A.PLANID = B.PLANID
  6. AND B.DEALERID = 1
  7. AND A.OFFERING = B.OFFERING
  8. ELSE
  9. SELECT A.AMOUNT , B.PLANID ,B.EVENTCD
  10. FROM ACCUMULATON A, PLAN B
  11. WHERE A.PLANID = B.PLANID
  12. AND B.DEALERID = 1
  13. ENDIF
  14. )
  15. LOOP ......
  16.  
  17.  
Nov 24 '06 #5
pragatiswain
96 Expert
Thanks.
Sorry, but IF --ELSE is not being accepted within FOR -- IN.
The following error coming up while compiling.
---------------------------------------------------------------------------
ORA-06550: line 7, column 4:
PLS-00103: Encountered the symbol "IF" when expecting one of the following:

( - + case mod new not null others select <an identifier>
<a double-quoted delimited-identifier> <a bind variable> avg
count current exists max min prior sql stddev sum variance
execute forall merge time timestamp interval date
<a string literal with character set specification>
<a number> <a single-quoted SQL string> pipe
--------------------------------------------------------------------------------

saby
Check if this works....

DECLARE
MY_SQL VARCHAR2;
SELECT 'SELECT A.AMOUNT , B.PLANID ,B.EVENTCD FROM ACCUMULATON A, PLAN B WHERE A.PLANID = B.PLANID AND B.DEALERID = 1 AND' || DECODE(AFLAG,1,'A.OFFERING = B.OFFERING','B.DEALERID = 1') INTO MY_SQL FROM DUAL

FOR CUR_A IN (MY_SQL)
LOOP ......

Other wise you have to use Dynamic SQL, using DBMS_SQL package.
Nov 24 '06 #6
suvam
31
Hi , u may try Sonic2k6 query or if U want not to use Union for performance issue, U may use this ----->

SELECT A.AMOUNT , B.PLANID ,B.EVENTCD
FROM ACCUMULATON A, PLAN B
WHERE A.PLANID = B.PLANID
AND B.DEALERID = 1
AND( (A.OFFERING = B.OFFERING AND aFlag = 1) OR(1=1)) ;
Dec 19 '06 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Phil Powell | last post by:
Here is the scope of what I need to do; want: enrollment_year allowed (even if null) all of ica criteria:
4
by: bobsawyer | last post by:
I've been building a series of SELECT lists that are populated dynamically using HTTPRequest. Things are going pretty well, and I've got the whole thing working flawlessly in Mozilla/Firebird....
2
by: GIS Analyst | last post by:
Hi to all I wish to be able to have a standard select statement which has additional fields added to it at run-time based on supplied parameter(s). ie declare @theTest1 nvarchar(10) set...
19
by: William Wisnieski | last post by:
Hello Everyone, I have a main form with a datasheet subform that I use to query by form. After the user selects two criteria on the main form and clicks the cmdShowResults button on the main...
31
by: Christopher Benson-Manica | last post by:
How about your if/else if/else constructs? Being nitpicky like any good C programmer, I'm in the process of transforming code written like if( cond ) { ... } else if( some_other_cond ) {...
2
by: Pavel Stehule | last post by:
Hello, Pg make query 1. and 2. very fast (use index), but for query 3. dont use index. I can solve its using select union, but I readed so pg 7.5 don't problem with OR operator. I use cvs pg. I...
3
by: Tim Smith | last post by:
I've been benchmarking some very simple databases. By simple, I mean a table like this: CREATE TABLE bench ( id SERIAL, data TEXT ) CREATE INDEX bench_data_index ON bench (data) which is...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
1
by: Lalit Sharma | last post by:
Hiii I am facing a proble that when i fire a query like - SELECT * FROM <TAB NAME> WHERE <COND> then no data found, whereas when I fire a query like - SELECT * FROM <TAB NAME> WHERE...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.