Oracle
帶有“綁定變數”的過程不返回所需的結果集
我有一個具有以下結構的表:
create table CUST_MODERN_CHANNEL_BRANCH ( customer_Num NUMBER, channel_Code NUMBER(5) )
一些範例數據:
Customer_Num Channel_Code ============= ============== 1 5 1 4 1 6 2 3 3 5
我寫了一個程序,你可以看到下面的程式碼:
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;
我
bind variable
在我的程序中使用它來保護它免受注射並使其性能更好。問題是,當我使用輸入參數測試我的程序時,第二列customer_num=1 / Order_by=2
的最終結果is not sorted
是這樣的:Customer_Num Channel_Code ============= ============== 1 5 1 4 1 6
但是當我使用 order by 編寫查詢時,它會給出以下輸出:
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
我的程序有問題嗎?還是我使用綁定變數的方式?提前致謝
order by 2
不等於order by :ord
哪裡:ord = 2
。,代表一個位置
order by 2
,2
這意味著結果必須始終按select
列表中的第二列排序。With
order by :ord
,:ord
是一個表達式,它將被評估為一個值,並且排序將按該值而不是列進行。如果您使用
order by :ord
where:ord = 2
,則處理方式類似order by '2'
(從邏輯上講,暫時忽略類型差異)。不要按位置綁定訂單。