2016-03-30 64 views
0

我將列名的csv作爲參數傳遞給pl/sql過程,而這個過程又將按sql命令的子句順序使用,但被忽略。將列的順序作爲參數傳遞給pl sql

SET serverOutput ON SIZE unlimited; 
SET linesize 32767; 
declare 
test1 varchar2(30); 
begin 
test1 := 'PARAMETER_NAME,DESCRIPTION'; -- This will be passed as input parameter 
for rCursor in (select * from configurations order by test1 asc) loop 
dbms_output.put_line(rCursor.parameter_name || '-=-' || rCursor.Description); 
-- Output is not ordered by any of these columns 
end loop; 
end; 

任何輸入?

+0

您的查詢「order by」是通過test1列,爲什麼您傾向於認爲您的數據將按參數名稱和Description列進行排序? ,要按此列進行排序,您需要更改查詢以包含按參數名稱排序,說明 – elirevach

回答

2

您正在使用一個變量來訂購靜態遊標,因此結果將是相同的比

select * from configurations order by 'PARAMETER_NAME,DESCRIPTION' 

如果您需要使用您的變量來動態地改變你的光標的順序,你可能需要的東西像這樣:

declare 
    test1 varchar2(30); 
    rcursor SYS_REFCURSOR; /* define a cursor */ 
    vConfiguration configurations%ROWTYPE; /* define a variable to host the result of your query */ 
begin 
    test1 := 'PARAMETER_NAME,DESCRIPTION'; -- This will be passed as input parameter 
    open rCursor for 'select * from configurations order by ' || test1; /* open a dynamic cursor */ 
    loop 
     fetch rCursor into vConfiguration; /* fetch the cursor into a variable */ 
     exit when rCursor%NOTFOUND;  /* check if the cursor has rows */ 
     dbms_output.put_line(vConfiguration.parameter_name || '-=-' || vConfiguration.Description); 
    end loop; 
end; 
+0

謝謝!它適用於我的要求。 –