根据列名作为查询条件来查询  要动态的 例如: 表A 里面有 字段 name ,age  这两个字段  
       我要查询表A中的name的值  可以这样  select  name from 表A;现在的问题是  要动态的  select   这里动态的传入列名  from  表A;

解决方案 »

  1.   

    用动态SQL去拼接,可以创建一个过程,参数为你传入的栏位名称p_column_name
    查询的sql用动态拼接:
    EXECUTE IMMEDIATE 'select '||p_column_name||' from A';
      

  2.   

    EXECUTE IMMEDIATE 'select '||p_column_name||' from A';   这句话 我要游标来读取 怎么读取啊?
      

  3.   

    用动态游标读取:type cur is ref cursor ;
    my_cur    cur;open my_cur for  'select '||p_column_name||' from A';
    ...
      

  4.   


    create or replace procedure proc_test(col varchar2, c in out sys_refcursor) is
    begin
      open c for 'select ' || col || ' from employees';
    end;
    -- 測試
    variable cr refcursordeclare
      cc sys_refcursor;
    begin
      proc_test('last_name', cc);
      :cr := cc;
    end;
    print cr
      

  5.   

    动态的一般是直接在程序中实现,而不需要在ORACLE数据库中来实现。
      

  6.   

    SQL> declare
      2  v_col varchar2(50);
      3  type cur_type is ref cursor;
      4  cur cur_type;
      5  col1 varchar2(50):='id';
      6  begin
      7  open cur for 'select  '||col1||' from tt' ;
      8  fetch cur into v_col;
      9  while cur%found loop
     10  dbms_output.put_line(v_col);
     11  fetch cur into v_col;
     12  end loop;
     13  close cur;
     14  end;
     15  /
    wkc168
    2
    2
    2
    2
    2
    8
    test
    11
    22
    44PL/SQL 过程已成功完成。
      

  7.   


    如何用 cursor cur is  'select '||p_column_name||' from A';
    这种游标来实现?
      

  8.   

    回楼上的,我给你指出了,你这个是动态查询,需要用动态SQL拼接,然后用动态游标读取,具体例子或者如何做,其实wkc168也说的很清楚了的啊具体点:
    create or replace procedure p(p_column_name in varchar2)
    is
      type    cur is ref cursor ;
      type    my_type is record(str varchar2(100));
      my_cur  cur;
      rs      my_type;
    begin
         open my_cur for 'select '||p_column_name||' from A';
         loop
             fetch my_cur into rs;
             exit when my_cur%notfound;
             dbms_output.put_line(rs.str);
         end loop;
         close my_cur;
    end;