I have a table with below structure:
create table CUST_MODERN_CHANNEL_BRANCH
(
customer_Num NUMBER,
channel_Code NUMBER(5)
)
Some example data :
Customer_Num Channel_Code
============= ==============
1 5
1 4
1 6
2 3
3 5
I wrote a procedure which you can see the code below :
create or replace procedure query_test(res out sys_refcursor,
order_by number,
customer_num number) is
qry varchar2(1000) := ' select * from cust_modern_channel_branch ';
begin
if customer_num is not null then
qry := qry || ' where customer_num = :cust ';
end if;
if order_by is not null then
qry := qry || ' order by :ord ';
end if;
--******* Generate Result
if customer_num is not null then
if order_by is not null then
open res for qry
using customer_num, order_by;
else
open res for qry
using order_by;
end if;
end if;
end;
I'm using bind variable
in my procedure to protect it from injection and make it better in terms of performance. The problem is that when I TEST my procedure with input parameters customer_num=1 / Order_by=2
the final result is not sorted
on the second column and it is like this:
Customer_Num Channel_Code
============= ==============
1 5
1 4
1 6
But when I write a query with order by, it gives me the output below:
select *
from CUST_MODERN_CHANNEL_BRANCH t
where t.customer_num = 1
order by 2
Customer_Num Channel_Code
============= ==============
1 4
1 5
1 6
Is that something wrong with my procedure? Or the way I used bind variables? Thanks in advance