Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I deal with null parameters in a PL/SQL stored procedure when I want to use them in comparisons?

Tags:

sql

oracle

plsql

I have a stored procedure with a parameter name which I want to use in a where clause to match the value of a column i.e. something like

where col1 = name

Now of course this fails to match null to null because of the way null works. Do I need to do

where ((name is null and col1 is null) or col1 = name)

in situations like this or is there a more concise way of doing it?

like image 881
Pip Falconer Avatar asked Oct 28 '10 11:10

Pip Falconer


People also ask

Can we pass NULL value in stored procedure?

You can't set impromptu to use a NULL value as the default prompt value, but you can work around this issue by setting the default value to a nonsensical value that would not be entered by a user normally, and then modfiying the stored procedure to change the parameter to NULL if it encounters that nonsensical value.

What is rule for NULL in Plsql?

The following three rules hold for null values: A null is never equal to anything else. None of the following IF statements can ever evaluate to TRUE: my_string := ' '; IF my_string = NULL THEN ...--This will never be true. max_salary := 0; IF max_salary = NULL THEN ...--This will never be true.

How does Oracle handle blank value?

You can use the NVL function to return a value when a null occurs. For example, the expression NVL(commission_pct,0) returns 0 if commission_pct is null or the value of commission_pct if it is not null.


2 Answers

You can use decode function in the following fashion:

where decode(col1, name, 0) is not null

Cite from SQL reference:

In a DECODE function, Oracle considers two nulls to be equivalent.

like image 133
andr Avatar answered Oct 20 '22 18:10

andr


I think your own suggestion is the best way to do it.

like image 44
Jokke Heikkilä Avatar answered Oct 20 '22 19:10

Jokke Heikkilä