I have a table with below structure and data :
create table TEST_TABLE
(
item_number NUMBER,
item_name VARCHAR2(50),
item_col VARCHAR2(50),
item_pol VARCHAR2(50)
)
Sample data:
item_number | item_name| item_col | item_pol
------------------------------------------------
1 | blue | b | c
2 | red | d | a
3 | black | e | a
4 | yellow | d | b
This is my sample procedure with in which I'm trying to use bind variables
.
create or replace procedure test_bind_variable(res out sys_refcursor,
item_number varchar2,
item_name varchar2,
item_col varchar2,
item_pol varchar2) is
qry varchar2(8000);
begin
qry := 'select * from test_table where 1=1 ';
if (item_number is not null) then
qry := qry || ' and item_number = :1 ';
end if;
if (item_name is not null) then
qry := qry || ' and item_name = :2 ';
end if;
if (item_col is not null) then
qry := qry || ' and item_col= :3 ';
end if;
if (item_pol is not null) then
qry := qry || ' and item_pol = :4 ';
end if;
dbms_output.put_line(qry);
open res for qry
using item_number, item_name, item_col, item_pol;
end;
The problem is that when all Input parameters have a values , the procedure works properly , without any error , but when only one or two parameters have value , I receive this error : ORA-01006: bind variable does not exist,
. How can I solve this problem? Some parameters might have value and some may not .
Thanks in advance