Oracle APEX: Multiple Conditions? - oracle

I wish to have a region displayed only if
SELECT * FROM REI_LABOUR_RATE_REQUEST
WHERE BILLING_PARTNER = :P6_FILTER_DEALER
returns at least one row and the element :P6_FILTER_YEAR is not NULL.
I tried this by writing:
SELECT * FROM REI_LABOUR_RATE_REQUEST
WHERE BILLING_PARTNER = :P6_FILTER_DEALER
AND :P6_FILTER_YEAR != NULL;
but that somehow never returns any row.

Did you try having a conditional display on the region with the type being "Value of item IS NOT NULL" and P6_FILTER_DEALER as the expression.
This should work as long as the value of the item is submitted and the item has the value stored in the session.
Region Source:
SELECT * FROM REI_LABOUR_RATE_REQUEST
WHERE BILLING_PARTNER = :P6_FILTER_DEALER

As #Tony Andrews says:
:P6_FILTER_YEAR != NULL will never be true. use :P6_FILTER_YEAR IS NOT
NULL instead. In fact the condition is redundant here, because
BILLING_PARTNER = :P6_FILTER_DEALER will only be true when
:P6_FILTER_YEAR is not null anyway.

Related

How to pass a null value as bind parameter

I have this table Line with a DEL_IND column. The possible values are Y or null
Im using OBIP, and there is a parameter requirement that allows selection of null or Y.
OBIP do not allow blank in their 'fixed value' menu.
I've tried to enter 'List of Values' in OBIP for No to be '' (empty string), but it doesnt seem to work.
LINE.DEL_IND = :P_DELETION_FLAG << i need to pass the value null for this clause
How do I pass null value selection into the query?
Even if you find the way to pass NULL, this:
WHERE LINE.DEL_IND = :P_DELETION_FLAG
won't work properly. If :P_DELETION_FLAG is NULL, query should look like this:
WHERE (LINE.DEL_IND = :P_DELETION_FLAG or :P_DELETION_FLAG IS NULL)
because
WHERE LINE.DEL_IND = NULL
is invalid; should be
WHERE LINE.DEL_IND IS NULL (or IS NOT NULL)

Linq statement fails with dynamic where clause

I want to retrieve commissions with a certain order number.
This works:
var expression = from commission in db.Auftraege
where commission.Auftragsnummer == orderNbr
select new Commission() { EF_Commission = (commission as Auftrag) };
return expression.ToList();
However, if i transform this to use a dynamic where clause (because i want to apply some more filters), the where-clause does not seem to be applied. Instead, all commissions are returned instead of only those with a specific number:
//base query
var expression = from commission in db.Auftraege select new Commission() { EF_Commission = (commission as Auftrag) };
//now add a where clause if the input parameter was specified
if (orderNbr >= 0)
expression.Where(commission => commission.EF_Commission.Auftragsnummer == orderNbr);
return expression.ToList();
I have looked at a dozen examples but they all seem to do it this way. Does anybody have an idea why the second query ignores the where clause?
You need to assign the interim expression to something (perhaps to itself). expression.Where() does not alter the existing query - it returns a new one.
So:
expression = expression.Where(...);

HQL Query with multiple Criteria

I am trying to write a HQL Query which selectes rows from a table based on multiple criteria.
firstName,lastName
the catch is that the query should be flexible to ignore any empty or null values
so
select t from table t where (:firstname = '' or t.firstName = :firstName) AND
(:lastName = '' OR t.lastName = :lastName)
I would have thought this would work? But it doesnt - it never returns any rows? Any ideas what could be wrong here? I am very new to HQL thats why this question.
If I am understanding correctly you want a way to allow the user to search by firstName, lastName or both. So you should be checking if the parameter passed in is empty then don't make it a condition. If they supply all blank parameters it would return the whole table. Try:
select t from table t
where (:firstname IS NULL or t.firstName = :firstName) AND
(:lastName IS NULL OR t.lastName = :lastName)
(:firstname = '' or t.firstName = :firstName)
Your criteria is strange. If :firstname = '' and if a firstname (t.firstName) is equal '' in the database, the criteria t.firstName = :firstName is good ('' = '')
You don't need :firstname = ''
But If you want to check null value, you need to do:
t.firstName IS NULL or t.firstName = :firstname
What happens if you run following hql with firstname parameter set to empty string?
select t from table t where (:firstname = '')
and following with firstname parameter set to null:
select t from table t where (:firstname is null)
If any of the above returns the whole table then the HQLs named parameter might support what you are trying to do.
Otherwise you must use different queries for the null parameter cases. You can do this by generating the query dynamically.
I had a similar requirement. I want dynamic but I'm using a tool that just gives an HQL editor, so no Java.
The query below allows for optional parameters. Essentially a pseudo-quazi XOR of sorts . . . wish there was real XOR :/
With this query you just put NA into a param instead of leaving it empty if it is not needed.
Yeah, yeah, yeah . . . it's ugly, but it works and it's easy to alter to any other scenario needing optional params in pure HQL.
SELECT t AS table
FROM Table t
WHERE (t.valSet = :valSet
AND (:category= 'NA' AND :subCategory= 'NA'))
OR (:category != 'NA'
AND (t.valSet = :valSet
AND t.category= :category))
OR (:subCategory != 'NA'
AND (t.valSet = :valSet
AND t.subCategory = :subCategory ))

How Oracle 10g evaluates NULL in boolean expressions

if not (i_ReLaunch = 1 and (dt_enddate is not null))
How this epression will be evaluated in Oracle 10g
when the input value of the i_ReLaunch = null and the value of the dt_enddate is not null
it is entering the loop.
According to the rules in normal c# and all it should not enter the loop as
it will be as follows with the values.
If( not(false and (true))
= if not( false)
=if( true) which implies it should enters the loop
But it is not happening
Can someone let me know if i am wrong at any place
Boolean operations with NULL value in Oracle return UNKNOWN - not true or false. So you have something like this:
If( not(UNKNOWN and (true)) = if not( UNKNOWN) =if( UNKNOWN )
In this case, IF will treat UNKNOWN as false.
If i_relaunch can be null, then you need to use some of NULL handling functions(NVL, NVL2, NULLIF, COALESCE, LNNVL) to be sure that you have correct result.
See these article for more information:
Nulls: Nothing to Worry About
Fundamentals of PL/SQL. Scroll down to - Handling Null Values in Comparisons and Conditional Statements

LINQ to dataset: CopyToDataTable()

I want to query a datatable (dt) and load a 2nd dt with the resultant collection of datarows. Fine - we have the CopyToDataTable() extn mthd for exactly that purpose. However it is constrained to enumerate only over DataRows, which means that I cannot return anything else e.g. a collection of anonymous types. So - how can I modify the values in the datarows?
Eg I have a dt with 3 columns: MyPK, VARCHAR01, VARCHAR02.
Foreach row, if VARCHAR01 or VARCHAR02 has the value "" (i.e. String.Empty) I want to replace that with NULL (which the underlying type allows).
I would do this as follows:
var MyCleanedDatarows =
from o in ds.Tables["dt"].AsEnumerable()
select new {
MyPK = o.Field<string>("MyPK"),
VARCHAR01 = (o.Field<string?>("VARCHAR01") == "" ? NULL : o.Field<string?>("VARCHAR01") ),
VARCHAR02 = (o.Field<string?>("VARCHAR02") == "" ? NULL : o.Field<string?>("VARCHAR02") )
};
...but then I cant use CopyToDataTable() to get back to a dt. I'm thinking I need to modify the datarows before invoking select operator, but I dont know how to achieve that. Any help/thoughts would be v.greatfully recieved.
Thanks in advance,
Tamim.
Take a look at this approach, in MSDN documentation.
http://msdn.microsoft.com/en-us/library/bb669096.aspx

Resources