oracle的select utc.column_name,hdw.questionid from user_tab_columns utc,question hdw where table_name = 'QUESTION' and hdw.questionid = 100925这样查出来是两列,一列是question表的字段,一列是id(100925),一样的。
我现在想一列是question表的字段,一列是对应字段再question表中的值。请问这个如何实现,谢谢!

解决方案 »

  1.   

    表结构这个不重要吧?
    假定question表为:
    ----------------------
    columnt_namequetionid
    name
    content
    answer
    ---------------------
    现在我想实现如下显示:
    ----------------------
    column_name   valuequetionid     100925
    name          Q1
    content       is this a question?
    answer        yes!
    ---------------------
      

  2.   

    declare
      type cur is ref cursor;
      c_emp cur;
      cn varchar2(100);
      sqlstr varchar2(1000);
      Ftable_name varchar2(100);
    begin
      sqlstr:='';
      Ftable_name:='bm';--table name
      open c_emp for select COLUMN_Name from user_tab_columns where table_name=upper(Ftable_name);
      loop
      fetch c_emp into cn;
      exit when c_emp%notfound;
      sqlstr:=sqlstr||' select '''||cn||''' as columnname,'||cn||' as columnvalue from bm union all';
      end loop;
      sqlstr:=substr(sqlstr,0,length(sqlstr)-10);
      --sqlstr:=sqlstr+' where id=100925';//查询条件
      
      dbms_output.put_line( sqlstr );
      --查询结果sql
      close c_emp;
    end;
      

  3.   

    declare
    type cur is ref cursor;
    c_emp cur;
    cn varchar2(100);
    sqlstr varchar2(1000);
    Ftable_name varchar2(100);
    begin
    sqlstr:='';
    Ftable_name:='question';--table name
    open c_emp for select COLUMN_Name from user_tab_columns where table_name=upper(Ftable_name);
    loop
    fetch c_emp into cn;
    exit when c_emp%notfound;
    sqlstr:=sqlstr||' select '''||cn||''' c1,'||cn||' c2 from '||Ftable_name||' union all';
    end loop;
    sqlstr:=substr(sqlstr,0,length(sqlstr)-10);
    dbms_output.put_line( sqlstr );
    --查询结果sql
    close c_emp;
    end;
    ///查询结果sql select 'QUETIONID' c1,QUETIONID c2 from question union all select 'NAME' c1,NAME c2 from question union all select 'CONTENT' c1,CONTENT c2 from question union all select 'ANSWER' c1,ANSWER c2 from question///
    QUETIONID 1
    NAME Q1
    CONTENT is this a question?
    ANSWER yes!
      

  4.   

    谢谢!上面那段暂时还看不是很明白,我先用下面的sql试试。
      

  5.   

    select utc.column_name,hdw.questionid from user_tab_columns utc left outer join question hdw on hdw.questionid = 100925  where table_name = 'QUESTION'