日期:2014-05-17  浏览次数:20702 次

问一个联合查询的SQL
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表中的值。

请问这个如何实现,谢谢!

------解决方案--------------------
表结构?
------解决方案--------------------
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;