Check if set of variables contains NULL in Oracle Apex PL/SQL - plsql

What is the most efficient/cleanest way to check if a set of variables in Oracle Apex includes a null value?
The obvious answer would be:
-- this is a PL/SQL Function in a Oracle Apex validation
v_item1 := :P1_ITEM1;
v_item2 := :P1_ITEM2;
(..)
if v_item1 is not null and v_item2 is not null ...
But is there a cleaner way that I'm not aware of?

I think the way you have it in your question is the best and most obvious way to do it, i.e.
if v_item1 is not null
and v_item2 is not null
...
However, there is another option:
if least (v_item1, v_item2, ...) is not null then
This works because least always returns null when any of its arguments is null.
Conversely, if you want to check several items are all null:
if v_item1 is null
and v_item2 is null
...
then you could use this:
if v_item1||v_item2||... is null then

Related

How do I fix not null constraints error on flutter?

I have an db which has two table: note and category. There is a schema below for my tables. I want to insert a record to the note table but I'm getting this error below. Can anyone please help?
Here is the method I create for insert a record to note table and the error which I'm getting from this process.
It clearly states that you declared your category_id as NOT NULL but you are passing a null value while trying to insert a note.
There may be 2 approach here,
You can simply remove NOT NULL constraint from your table definition by modifiying "category_id" INTEGER NOT NULL DEFAULT 1 to "category_id" INTEGER DEFAULT 1
You should really be sure that you are passing a non-null value to category_id in insert operation
As I see from your logs, it clearly says that one of the argument you are passing is null and it causes this crash.
Hope these helps you to detect your problem and fix it.

How to catch a failed CAST statement in BigQuery SQL?

We are converting a STRING field to a DATETIME field using BigQuery's non-legacy SQL.
The DATETIME fields are corrupted with values like "None" and "0.0" which causes our CAST statement to fail.
We see that for other types of SQL there are TRY-CATCH functions and ISNUMERIC() tests - neither of which appear to be supported in BigQuery.
Here's an example that catches "None" but fails to catch random floats or integers:
CASE
WHEN UPDT_DT_TM LIKE 'None' THEN NULL
ELSE CAST(UPDT_DT_TM AS DATETIME)
END AS UPDT_DT_TM,
Apart from User-Defined-Functions (UDF) in BigQuery - is there any other way in create a CASE statement that can convert to DATETIME when it can but otherwise just leaves the value as NULL?
You can use the SAFE_CAST function, which returns NULL if the input is not a valid value when interpreted as the desired type. In your case, you would just use SAFE_CAST(UPDT_DT_TM AS DATETIME). It is in the Functions & Operators documentation.

PL/SQL: Does SELECT INTO give the variable NULL if the value is NULL in the table?

I ask this question because I am trying to make a function that returns a value based on if the selected information in the table is NULL or not. However, I cannot get the argument in the if statement to be true.
So, I have a VARCHAR2 variable prepared, and then I use a select statement
SELECT info INTO lv_test
WHERE ...
IF lv_test != NULL THEN
--Do not NULL stuff
ELSE
--Do NULL stuff
Now, in the code I use specifically, I get values into lv_test, but unless I check for them directly, like IF lv_test = 'NY', which becomes true and works, it won't become true. It will not work whenever I check for NULL values, nor does it work if I check for it to be = '' (initialized/default value).
My experience in SQL is fairly limited so this could be something basic I simply don't know yet. So to answer my questions:
How do you check for NULL values from a SELECT ... INTO ... statement?
Why does my argument not work?

Check if record exists, if it does add 1 (inside procedure)

I am trying to check if an email address already exists, and add 1 if it does (and 1 if even the email+1 exists and so on). But so far I can't even figure out how to check if it exists, inside a procedure.
if exists (select 1 from table where email='something') then ...
Gives back an error ("function or pseudo-column 'EXISTS' may be used inside a SQL statement only)". Tried other stuff as well, but those might not be worth mentioning.
After I have this I plan on making a while loop for adding 1 as much as needed.
You can select the number of matching records into a variable (which you have declared), and then check that variable's value:
select count(*) into l_count
from my_table
where email = 'something';
if l_count > 0 then
-- record exists
...
else
-- record does not exist
...
end if;
select ... into always has to get exactly one record back, and using the count aggregate function means that happens, evenif more than one matching record exists.
That hopefully covers your specific issue about checking for existance. As for your underlying goal, it sounds like you're trying to find an unused value by incrementing a suffix. If so, this similar question might help. That is looking for usernames rather than emails,but the principle is the same.
As pointed out in comments, simultaneous calls to your procedure might still try to use the same suffix value; so you still need a unique constraint, and handling for that being violated in that scenario. I think that's beyond what you asked about here though.

May Cassandra maps hold null values

May a Cassandra (CQL 3) map hold null values? I thought null values were permitted, but failure of my program suggests otherwise. Or is there a bug in the driver I am using?
The official documentation for CQL maps says:
A map is a typed set of key-value pairs, where keys are unique. Furthermore, note that the map are internally sorted by their keys and will thus always be returned in that order.
So the keys may not be null (otherwise sorting would be impossible), but there is no mention of a requirement that map values are not null.
I have a field that is a map<timestamp,uuid>, which I am trying to write to using values in a Java Map< Date, UUID >. One of the map values (UUIDs) is null. This seems to cause a NPE in the Cassandra client code (Cassandra version 1.2.6, called from DataStax Java driver 1.0.1) when marshalling the UUID of the map:
java.lang.NullPointerException
at org.apache.cassandra.utils.UUIDGen.decompose(UUIDGen.java:82)
at org.apache.cassandra.cql.jdbc.JdbcUUID.decompose(JdbcUUID.java:55)
at org.apache.cassandra.db.marshal.UUIDType.decompose(UUIDType.java:187)
at org.apache.cassandra.db.marshal.UUIDType.decompose(UUIDType.java:43)
at org.apache.cassandra.db.marshal.MapType.decompose(MapType.java:122)
at org.apache.cassandra.db.marshal.MapType.decompose(MapType.java:29)
at com.datastax.driver.core.BoundStatement.bind(BoundStatement.java:188)
at [my method]
The UUIDGen.decompose(UUID) method has no special handling of a null UUID, hence the NPE. Contrast with JdbcBoolean.decompose(Boolean), which decomposes a null Boolean to an empty byte-buffer. Similarly, JdbcDate.decompose(Date) decomposes a null Date to an empty byte-buffer.
I can produce a similar problem if I have a map holding null integers (using a Java Map< Date, Integer > with a null value, for a Cassandra map<timestamp,int>), so this problem is not restricted to uuid values.
You are right, null values are not (yet?) supported inside Maps. I faced this thing before and like you couldn't find relative documentation -- In similar situation I help myself with cqlsh
A small test give you the answer
CREATE TABLE map_example (
id text,
m map<text, text>,
PRIMARY KEY ((id))
)
try
insert into map_example (id, m ) VALUES ( 'a', {'key':null});
> Bad Request: null is not supported inside collections
HTH, Carlo

Resources