How to ignore null parameter in a Stored Procedure Oracle - oracle

I have created a stored procedure where the user can inset 1 or multiple values in the parameter
create or replace procedure MyProcerdure
(
title Film.Title%Type,
country Film.country%Type,
language Film.language%Type,
category Film.category%Type,
refCursor OUT SYS_REFCURSOR )
AS
begin
OPEN refCursor FOR
select Film.Title as FilmTitle,
Film.language as language
Film.category
FROM Film
Where Film.language=language
AND Film.category=category
AND Film.Country=country
//etc...
But I want to allow the fact that the user doesn't have to fill them all and pass them in parameter , which means if the user only enter the language without anything else , return the proper language , and let's say he entered country and language so the result should get WHERE language and country is equal to what he inserted
Is it possible to make such a mechanism in a stored procedure using oracle ?
Thank you

You can simply add some logic in your query to handle the fact that parameters can be null:
CREATE OR REPLACE PROCEDURE MyProcerdure(
p_title Film.Title%TYPE,
p_country Film.country%TYPE,
p_language Film.language%TYPE,
p_category Film.category%TYPE,
po_refCursor OUT SYS_REFCURSOR
) AS
BEGIN
OPEN po_refCursor FOR
SELECT Film.Title AS FilmTitle, Film.language AS language, Film.category
FROM Film
Where ( p_title is null or Film.title = p_title )
AND ( p_country is null or Film.country = p_country )
AND ( p_language is null or Film.language = p_language )
AND ( p_category is null or Film.category = p_category );
END;

The best way to accomplish this is to use dynamic SQL. You can conditionally concatenate the correct filters. You will also want to concatenate an alternate version for when no value was provided.
For example, the following allows you to either filter, or put in a statement that the compiler will ignore but still has the correct number of bind variables in the dynamic SQL.
CREATE OR REPLACE PROCEDURE MyProcerdure
(
title file.title%TYPE,
country file.country%TYPE,
language file.language%TYPE,
category file.category%TYPE,
refCursor OUT SYS_REFCURSOR
) IS
l_stmt VARCHAR2(4000);
BEGIN
l_stmt := 'SELECT f.title AS filetitle,'||
' f.language AS language,'||
' f.category'||
' FROM file f'||
' WHERE 1 = 1';
IF title IS NOT NULL THEN
l_stmt := l_stmt || ' AND f.title = :title';
ELSE
l_stmt := l_stmt || ' AND (1=1 OR :title IS NULL)';
END IF;
-- The others would be done similarly
OPEN refCursor FOR l_stmt USING title, -- The others would go the same order as above
END;
/

Related

PL/SQL - pass query in for loop stored in variable

i have a scenario where i need to create a dynamic query with some if/else statements, i have prepared the query but unable to use this inside loop below is the snippet of what i am trying to acheive.
Query :
select user_name into username from table1 where user ='user1';
for(query)
Loop
Dbms_output.put_line('user name ' || user_name);
END Loop;
is this possible to use the vaiable in the for loop ?
Not really. Notice in the 19c PL/SQL Reference it says:
cursor
Name of an explicit cursor (not a cursor variable) that is not open when the cursor FOR LOOP is entered.
You have to code this the long way, by explicitly fetching the cursor into a record until you hit cursorname%notfound, e.g.
create or replace procedure cheese_report
( cheese_cur in sys_refcursor )
as
type cheese_detail is record
( name varchar2(15)
, region varchar2(30) );
cheese cheese_detail;
begin
loop
fetch cheese_cur into cheese;
dbms_output.put_line(cheese.name || ', ' || cheese.region);
exit when cheese_cur%notfound;
end loop;
close cheese_cur;
end;
Test:
declare
cheese sys_refcursor;
begin
open cheese for
select 'Cheddar', 'UK' from dual union all
select 'Gruyere', 'France' from dual union all
select 'Ossau Iraty', 'Spain' from dual union all
select 'Yarg', 'UK' from dual;
cheese_report(cheese);
end;
/
Cheddar, UK
Gruyere, France
Ossau Iraty, Spain
Yarg, UK
In Oracle 21c you can simplify this somewhat, though you still have to know the structure of the result set:
create or replace procedure cheese_report
( cheese_cur in sys_refcursor )
as
type cheese_detail is record
( cheese varchar2(15)
, region varchar2(30) );
begin
for r cheese_detail in values of cheese_cur
loop
dbms_output.put_line(r.cheese || ', ' || r.region);
end loop;
end;
You could parse an unknown ref cursor using dbms_sql to find the column names and types, but it's not straightforward as you have to do every processing step yourself. For an example of something similar, see www.williamrobertson.net/documents/refcursor-to-csv.shtml.

PL SQL : Create oracle record dynamically

I dont know if it is possible but I would like to do this in PL/SQL
Let's say I have a parameter in my procedure, a number : numberColumns.
Inside the procedure I would like to create a record :
TYPE arrayColumn IS RECORD (
column1 VARCHAR2(200),
column2 VARCHAR2(200)...
... as much à numberColumns value
....
);
ty
This can be achieved easily with with OBJECT type in Oracle. But this kind of architecture is not at all suggested. Hope this below solution helps.
CREATE OR REPLACE
PROCEDURE test_obj_form(
a NUMBER )
AS
lv_sql VARCHAR2(32676);
BEGIN
SELECT '('
||listagg(str,',') WITHIN GROUP (
ORDER BY lvl)
||')'
INTO lv_sql
FROM
(SELECT 1 dum,
'column'
||LEVEL
||' '
||'varchar2(200)' str,
level lvl
FROM dual
CONNECT BY LEVEL < A
ORDER BY LEVEL
)
GROUP BY dum;
EXECUTE IMMEDIATE 'CREATE OR REPLACE TYPE name_rec IS OBJECT '||lv_sql;
dbms_output.put_line(lv_sql);
END;
/
Rather than using a record, you could use a collection:
CREATE OR REPLACE TYPE stringlist IS TABLE OF VARCHAR2(4000);
/
Then do:
CREATE OR REPLACE PROCEDURE your_procedure(
number_columns IN INTEGER,
values OUT stringlist
)
IS
BEGIN
values := stringlist();
IF number_columns < 1 THEN
RETURN;
END IF;
values.EXTEND( number_columns );
FOR i IN 1 .. number_columns LOOP
values(i) := DBMS_RANDOM.STRING( 'X', 100 ); -- Assign some value
END LOOP;
END;
/
Otherwise, if you really want a record, then you will have to result to dynamic SQL.

Stored Procedure PL SQL If String Is blank

I got a problem with Oracle Stored Procedure. The if else statement didnt check whether the string is blank or not. Or am I doing it wrong?
create or replace PROCEDURE GET_ICECREAM
(
flavour IN VARCHAR2,
toppings IN VARCHAR2,
cursorIC OUT sys_refcursor
)
AS
dynaQuery VARCHAR2(8000);
BEGIN
dynaQuery := 'SELECT price FROM tblIceCream';
IF flavour <> '' THEN
dynaQuery := dynaQuery || ' WHERE flavour LIKE '''%''' '
ENDIF
OPEN cursorIC FOR dynaQuery;
END GET_ICECREAM;
DISCLAIMER: Above is not actual stored procedure. I'm using an example to understand concept of if else and native dynamic SQL in Oracle. So that its easier for you guys to understand ;)
Hope this below snippet will help you to understand how to handle empty string and NULL values.
SET serveroutput ON;
DECLARE
lv_var VARCHAR2(100):='';
BEGIN
IF lv_var IS NULL THEN
dbms_output.put_line('is null');
ELSE
dbms_output.put_line('av');
END IF;
END;
------------------------------------OUTPUT--------------------------------------
PL/SQL procedure successfully completed.
is null
--------------------------------------------------------------------------------
SET serveroutput ON;
DECLARE
lv_var VARCHAR2(100):='';
BEGIN
IF lv_var = '' THEN
dbms_output.put_line('is null');
ELSE
dbms_output.put_line('av');
END IF;
END;
--------------------------------------output-----------------------------------
PL/SQL procedure successfully completed.
av
--------------------------------------------------------------------------------
In PL/SQL, a zero length string that is assigned to a varchar2 variable is treated as a NULL.
In your case, if argument flavour is assigned a zero length string, the line below is actually comparing a NULL to something, which is always false.
IF flavour <> '' then
Fix that line according to your business logic to take care of null values for flavour, and you'll be fine. An example fix would be:
if flavour is not null then
In Oracle, the empty string is equivalent to NULL.
So you want to do:
IF flavour IS NOT NULL THEN
However, a better solution is not to use dynamic SQL but to re-write the WHERE filter:
create or replace PROCEDURE GET_ICECREAM
(
flavour IN VARCHAR2,
topping IN VARCHAR2,
cursorIC OUT sys_refcursor
)
AS
BEGIN
OPEN cursorIC FOR
SELECT price
FROM tblIceCream
WHERE ( flavour IS NULL OR your_flavour_column LIKE '%' || flavour || '%' )
AND ( topping IS NULL or your_topping_column LIKE '%' || topping || '%' );
END GET_ICECREAM;
/
You can try like this:
IF NVL(flavour, 'NULL') <> 'NULL'

Oracle - change columns in WHERE clause based on input

I want use separate columns in WHERE clause based on the INPUT received in the stored procedure.
If TYPE_DEFINITION = 'SUP' then use SUPPLIER column
If TYPE_DEFINITION = 'CAT' then use CATEGORY column
I know I can write two separate SELECT's using a CASE statement, but that will be very dumb and redundant. Any cleaner way of doing it?
CREATE OR REPLACE PROCEDURE SG.STORED_PROCEDURE (
TYPE_DEFINITION IN VARCHAR2,
VALUE IN VARCHAR2,
STORELIST IN VARCHAR2)
AS
BEGIN
SELECT O.ORGNUMBER,
S.SKU,
FROM SKU S JOIN ORG O ON S.ORGID = O.ORGID
WHERE
AND O.ORGNUMBER IN (STORELIST)
AND (CASE TYPE_DEFINITION
WHEN 'SUP' THEN S.SUPPLIER = VALUE
ELSE S.CATEGORY = VALUE
END);
END;
/
Your code is very close. The CASE THEN must return an expression, not a condition. But the CASE can be used as part of a condition, just move the = VALUE
to the outside.
Change this:
AND (CASE TYPE_DEFINITION
WHEN 'SUP' THEN S.SUPPLIER = VALUE
ELSE S.CATEGORY = VALUE
END);
To This:
AND VALUE = (CASE TYPE_DEFINITION
WHEN 'SUP' THEN S.SUPPLIER
ELSE S.CATEGORY
END);
Your code makes sense. This limitation is probably a result of Oracle not fully supporting Booleans.
UPDATE
If you run into performance problems you may want to use dynamic SQL or ensure that the static SQL is correctly using FILTER operations. When Oracle builds an execution plan it is able to use bind variables like constants, and choose a different plan based on the input. As Ben pointed out, these FILTER operations don't always work perfectly, sometimes it may help if you use simplified conditions like this:
(TYPE_DEFINITION = 'SUP' AND S.SUPPLIER = VALUE)
OR
((TYPE_DEFINITION <> 'SUP' OR TYPE_DEFINITION IS NULL) AND S.CATEGORY = VALUE)
You need to use dynamic sql in your procedure.
Something like this:
CREATE OR REPLACE PROCEDURE SG.STORE_PROC (
TYPE_DEFINITION IN VARCHAR2,
VALUE IN VARCHAR2,
STORELIST IN VARCHAR2)
AS
TYPE EmpCurTyp IS REF CURSOR;
v_emp_cursor EmpCurTyp;
v_stmt_str VARCHAR2(200);
v_orgnumber VARCHAR2(200);
v_sku VARCHAR2(200);
BEGIN
v_stmt_str := 'SELECT O.ORGNUMBER, S.SKU,FROM SKU S JOIN ORG O ON S.ORGID = O.ORGID ';
if type_definition = 'SUP' then
v_stmt_str := v_stmt_str || 'WHERE s.supplier = :v';
else
v_stmt_str := v_stmt_str || 'WHERE s.category = :v';
end if;
-- Open cursor & specify bind variable in USING clause:
OPEN v_emp_cursor FOR v_stmt_str USING value;
-- Fetch rows from result set one at a time:
LOOP
FETCH v_emp_cursor INTO v_orgnumber, v_sku;
-- you can do something here with your values
EXIT WHEN v_emp_cursor%NOTFOUND;
END LOOP;
-- Close cursor:
CLOSE v_emp_cursor;
END;
/

Oracle dynamic parameters

I'm struggling to create a dynamic sql parametrized query. It involves using 'IS NULL' or 'IS NOT NULL'
Here's a simple pl/sql query:
CREATE OR REPLACE PROCEDURE GET_ALL_INFORMATION
(
"PARAM_START_DATE" IN DATE,
"PARAM_END_DATE" IN DATE,
"PARAM_IS_SUBMITTED" IN NUMBER,
"EXTRACT_SUBMITTED_CONTACTS" OUT sys_refcursor
) IS
sql_stmt VARCHAR2(3000);
PARAM_CONDITION VARCHAR2(20);
BEGIN
IF PARAM_IS_SUBMITTED = 1 THEN
PARAM_CONDITION := 'NOT NULL';
ELSE
PARAM_CONDITION := 'NULL';
END IF;
sql_stmt := ' SELECT
REGISTRATION_NUMBER,
NAME PROVIDER_TYPE,
ORGANIZATION
FROM TABLE_A
WHERE
P.DATE_FINALIZED IS :A;
OPEN EXTRACT_SUBMITTED_CONTACTS FOR sql_stmt USING PARAM_CONDITION;
Whereas the parameter (:A) in (USING PARAM_CONDITION) should have 'NULL' or 'NOT NULL'. It does not seem to work the way I envisioned.
Am I missing something?
As explained by GriffeyDog in a comment above, bind parameters could only be used as place holder for values. Not to replace keywords or identifiers.
However, this is not really an issue here, as you are using dynamic SQL. The key idea ifs that you build your query as a string -- and it will be parsed at run-time by the PL/SQL engine when you invoke EXECUTE or OPEN .. FOR.
Simply said, you need a concatenation -- not a bound parameter:
...
sql_stmt := ' SELECT
REGISTRATION_NUMBER,
NAME PROVIDER_TYPE,
ORGANIZATION
FROM TABLE_A
WHERE
P.DATE_FINALIZED IS ' || PARAM_CONDITION;
-- ^^
OPEN EXTRACT_SUBMITTED_CONTACTS FOR sql_stmt;

Resources