473,395 Members | 1,530 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,395 software developers and data experts.

conditional selection of 'AND'

I have one query similar to the following:-

select coursenum,sectionnum,instructor from
SECTION s where (sectionnum = '001')
and coursenum LIKE 'MATH%'
but is it possible to add another 'and' condition to the above query
provided a certain user parameter = 'add'

for e.g

select coursenum,sectionnum,instructor from
SECTION s where (sectionnum = '001')
and coursenum LIKE 'MATH%'
and instructor LIKE 'L%' i.e. only and only if :parameter = 'add' else
the above query gets executed.

I do not want to use procedure for the above as I think everyting can
be done using and,or,not
Jul 19 '05 #1
6 6623
Rohit Dhawan wrote:
I have one query similar to the following:-

select coursenum,sectionnum,instructor from
SECTION s where (sectionnum = '001')
and coursenum LIKE 'MATH%'
but is it possible to add another 'and' condition to the above query
provided a certain user parameter = 'add'

for e.g

select coursenum,sectionnum,instructor from
SECTION s where (sectionnum = '001')
and coursenum LIKE 'MATH%'
and instructor LIKE 'L%' i.e. only and only if :parameter = 'add' else
the above query gets executed.

I do not want to use procedure for the above as I think everyting can
be done using and,or,not


Yes, but only PL/SQL & not in SQL*PLUS.
Partly because straight SQL does not support the "IF".

Jul 19 '05 #2
AnaCDent <an*******@hotmail.com> wrote in message news:<qekgc.55083$U83.8534@fed1read03>...
Rohit Dhawan wrote:
I have one query similar to the following:-

select coursenum,sectionnum,instructor from
SECTION s where (sectionnum = '001')
and coursenum LIKE 'MATH%'
but is it possible to add another 'and' condition to the above query
provided a certain user parameter = 'add'

for e.g

select coursenum,sectionnum,instructor from
SECTION s where (sectionnum = '001')
and coursenum LIKE 'MATH%'
and instructor LIKE 'L%' i.e. only and only if :parameter = 'add' else
the above query gets executed.
I do not want to use procedure for the above as I think everyting can
be done using and,or,not


Yes, but only PL/SQL & not in SQL*PLUS.
Partly because straight SQL does not support the "IF".


Dear Rohit,
Keep in mind one thing : you cannot control execution of query in SQL
prompt instead you have to return null(empty) result. you can achieve this
by using insertion in temp table and join.
e.g.
insert into temp(singleColumn) values (:parameter);
select s.coursenum,s.sectionnum,s.instructor from
SECTION s,temp t where (sectionnum = '001')
and coursenum LIKE 'MATH%'
and t.param = 'add';

if you want a better way try this but I am not sure whether it will work on
oracle.
select coursenum,sectionnum,instructor,:parameter p from
SECTION s where (sectionnum = '001')
and coursenum LIKE 'MATH%'
and p='add';
cheers and welcome
Jul 19 '05 #3
Jan
Hope this help:

select coursenum,sectionnum,instructor
FROM SECTION
WHERE (sectionnum = '001')
AND coursenum LIKE 'MATH%'
AND (CASE WHEN &my_param='add' THEN coursenum ELSE '1' END)
LIKE (CASE WHEN &my_param='add' THEN 'L%' ELSE '1' END)
Jan

ro**********@yahoo.com (Rohit Dhawan) wrote in message news:<c7**************************@posting.google. com>...
I have one query similar to the following:-

select coursenum,sectionnum,instructor from
SECTION s where (sectionnum = '001')
and coursenum LIKE 'MATH%'
but is it possible to add another 'and' condition to the above query
provided a certain user parameter = 'add'

for e.g

select coursenum,sectionnum,instructor from
SECTION s where (sectionnum = '001')
and coursenum LIKE 'MATH%'
and instructor LIKE 'L%' i.e. only and only if :parameter = 'add' else
the above query gets executed.

I do not want to use procedure for the above as I think everyting can
be done using and,or,not

Jul 19 '05 #4

"niranjan v sathe" <sa***********@yahoo.com> wrote in message
news:c3*************************@posting.google.co m...
| AnaCDent <an*******@hotmail.com> wrote in message
news:<qekgc.55083$U83.8534@fed1read03>...

....
|
| Dear Rohit,
| Keep in mind one thing : you cannot control execution of query in
SQL
| prompt instead you have to return null(empty) result. you can achieve
this
| by using insertion in temp table and join.
| e.g.
| insert into temp(singleColumn) values (:parameter);
| select s.coursenum,s.sectionnum,s.instructor from
| SECTION s,temp t where (sectionnum = '001')
| and coursenum LIKE 'MATH%'
| and t.param = 'add';
|

sorry, not a good approach

the temporary table (which in oracle would need to be a GLOBAL TEMPORARY
TABLE, which is a permanent object with temporary contents) is not necessary

see the other posts that recommend CASE (or decode if you are pre-9i)

| if you want a better way try this but I am not sure whether it will work
on
| oracle.
| select coursenum,sectionnum,instructor,:parameter p from
| SECTION s where (sectionnum = '001')
| and coursenum LIKE 'MATH%'
| and p='add';
| cheers and welcome

this query would only work if the p is set to lower case 'add' -- if it is
anything else, then it will return no rows

it would need to be written like this (also note that the column alias
cannot be referenced in a predicate)

select
coursenum,sectionnum,instructor,:parameter p
from
SECTION s
where
sectionnum = '001'
and
(
(
coursenum LIKE 'MATH%'
and
:parameter ='add'
)
or
:parameter is null
)

but, see my other post in comp.database.oracle.misc on why the 'add'
variable is unnecessary

(also, Rohit, _please_ remember to not cross-post -- this thread is now
living in more than one forum)

;-{ mcs
Jul 19 '05 #5
ro**********@yahoo.com (Rohit Dhawan) wrote in message news:<c7**************************@posting.google. com>...
I have one query similar to the following:-

select coursenum,sectionnum,instructor from
SECTION s where (sectionnum = '001')
and coursenum LIKE 'MATH%'
but is it possible to add another 'and' condition to the above query
provided a certain user parameter = 'add'

for e.g

select coursenum,sectionnum,instructor from
SECTION s where (sectionnum = '001')
and coursenum LIKE 'MATH%'
and instructor LIKE 'L%' i.e. only and only if :parameter = 'add' else
the above query gets executed.

I do not want to use procedure for the above as I think everyting can
be done using and,or,not


You could try something like this:

select coursenum,sectionnum,instructor from
SECTION s where (sectionnum = '001')
and coursenum LIKE 'MATH%'
and ( ( instructor LIKE 'L%'
and :parameter = 'add' )
OR
( :parameter != 'add' )
)
Jul 19 '05 #6

"Rohit Dhawan" <ro**********@yahoo.com> wrote in message
news:c7**************************@posting.google.c om...
I have one query similar to the following:-

select coursenum,sectionnum,instructor from
SECTION s where (sectionnum = '001')
and coursenum LIKE 'MATH%'
but is it possible to add another 'and' condition to the above query
provided a certain user parameter = 'add'

for e.g

select coursenum,sectionnum,instructor from
SECTION s where (sectionnum = '001')
and coursenum LIKE 'MATH%'
and instructor LIKE 'L%' i.e. only and only if :parameter = 'add' else
the above query gets executed.

I do not want to use procedure for the above as I think everyting can
be done using and,or,not


I hope you have a better reason for not doing this in pl/sql than that you
"do not want to use a procedure".
IMO the pure sql solution is doable (using case or decode/nvl) but "ugly"
due to the fact, that some developers
will find it hard to grasp easily and maintain if they take over the project
later.
Personally I'd solve this using a ref-cursor which would
a: give you complete flexibility allowing you to construct your query at
runtime, meaning you'd be able to change
more than just the "and" parts of the query
b: give you much of the functionality inherent in explicit cursors

Bear in mind that I haven't been working with Oracle-stuff for that long and
am still learning new stuff everyday...

Cheers

Morten Olsson
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: chandy | last post by:
Hi, I am trying to change an SP from dynamic SQL to proper SQL but I can't figure a way to conditionally add extra parts to the statement. How can I do the equivalent of the following? ...
9
by: pk | last post by:
Here is my problem. I want to make a webapp that will basically take the work out of finding what tool works for what situation. There are 5 factors that go into tool selection. 1)Material...
3
by: B Love | last post by:
I would like to display a drop-down list on a form conditional on the value of another field on the same form. I am not sure if this is good form or not. I am using Access97, but have access to...
2
by: Jeff Mason | last post by:
Hi, I'm trying to construct a query (in a stored procedure) which will have a number of selection criteria based on input parameters. There are a number of these parameters whose selection...
8
by: Typehigh | last post by:
I have many text fields with conditional formatting applied, specifically when the condition is "Field Has Focus". Without any events associated with the fields the conditional formatting works...
5
by: paulo | last post by:
Can anyone please tell me how the C language interprets the following code: #include <stdio.h> int main(void) { int a = 1; int b = 10; int x = 3;
43
by: dev_cool | last post by:
Hello friends, I'm a beginner in C programming. One of my friends asked me to write a program in C.The purpose of the program is print 1 to n without any conditional statement, loop or jump. ...
5
by: NewtoAccess | last post by:
Hi. I have a FORMA with a BUTTON1 which opens FORMB based on the value of PORTID field as a setlink criteria. If No record is found during setlinkcriteria, then I would like the PORTID value of...
10
by: afromanam | last post by:
Regards, Please help What I'm trying to do is this: (and I can't use reports since I must export to Excel) I export some queries to different tabs in an excel workbook I then loop through...
1
by: Greg (codepug | last post by:
Access 2000 Using a textbox of a single form, I created a calculated field. The following code is in the Control Source for this field: =IIf(=24,+(/),/ ) The numbers that are calculated are...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.