将列顺序作为参数传递给 pl sql

Passing order by columns as argument to pl sql

我正在将列名的 csv 作为 pl/sql 过程的参数传递,该过程又将按 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;

有任何输入吗?

您正在使用变量对静态游标进行排序,因此结果与

相同
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;