Help with oracle query/procedure using null values
Hi all,
I have the following query which is running fine:
select *
from MyTable
where Column1 = 'A' and
Column2 = 'B' and
Column3 = 'C' and
Column4 is null and
Column5 is null and
Column6 = 3;
It returns one row. But when I convert the above into the following:
select *
from MyTable
where Column1 = 'A' and
Column2 = 'B' and
Column3 = 'C' and
Column4 = null and
Column5 = null and
Column6 = 3;
Its returning nothing. How can I make it work the second way? I actually need to pass the values for the colums through variables and place the same query into a stored procedure. How can I make this work.
Thanks for the reply. I want to use the above query in a stored procedure which have null values for the parameters as below:
create or replace
procedure MyProcedure
(val1 in varchar2 default null,
val2 in varchar2 default null,
val3 in varchar2 default null,
val4 in varchar2 default null,
val5 in varchar2 default null,
val6 in varchar2 default null,
val7 in varchar2 default null,
val8 in varchar2 default null,
cCount in number,
DataSet OUT Types.cursor_type)
as
begin
OPEN DataSet FOR
select CID
from MyTable
where Column_1 = val1 and
Column_2 = val2 and
Column_3 = val3 and
Column_4 = val4 and
Column_5 = val5 and
Column_6 = val6 and
Column_7 = val7 and
Column_8 = val8 and
Column_9 = Count;
end;
My paramters will sometimes have values and some times null. I can not use IS NULL all the time. Please advise.
create or replace
procedure GetData
(POS1 in varchar2 default null,
POS2 in varchar2 default null,
POS3 in varchar2 default null,
POS4 in varchar2 default null,
POS5 in varchar2 default null,
POS6 in varchar2 default null,
POS7 in varchar2 default null,
POS8 in number)
as
cid number(9,0);
begin
select MYTESTTABLE.id into cid
from MYTESTTABLE
where COLUMN1 = POS1 and
COLUMN2 = POS2 and
COLUMN3 = POS3 and
COLUMN4 = POS4 and
COLUMN5 = POS5 and
COLUMN6 = POS6 and
COLUMN7 = POS7 and
COLUMN8 = POS8;
DBMS_OUTPUT.PUT_LINE(CID);
end;
Whenever you check column with equal to (=) NULL it doesnt return any value.
NULL columns should always be checked with IS NULL clause.
So the modified query will be
select *
from MyTable
where Column1 = 'A' and
Column2 = 'B' and
Column3 = 'C' and
Column4 IS null and
Column5 IS null and
Column6 = 3;
Bookmarks