首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > oracle >

问一个联合查询的SQL,该如何解决

2012-01-30 
问一个联合查询的SQLoracle的selectutc.column_name,hdw.questionidfromuser_tab_columnsutc,questionhdww

问一个联合查询的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;

热点排行