大家帮我看看呀,很着急!
我有一张表A,A表中存在70多条记录,表结构如下:
id   stationname   stationID
1    新货站        M9010
2    城市站        M9011另有70多各表,表结构都相同,每个表的名称如:Temp_M9010,Temp_M9011,表结构如下:
表:Temp_M9010
id  value1 value2 value3
1      3    3       3
2      5    5       5表:Temp_M9011
id  value1 value2 value3
1      4    4       4
2      6    6       6
现在想创建这样一个视图,接结果如下:
id  stationname   stationID   value1   value2  value3
1   新货站        M9010       3        3       3
2   城市站        M9011       4        4       4即想实现第一张表和其余70多张表的第一条记录相连接,构成一个新视图。因为我只有发80分帖子的权限,还望大家不要介意呀!

解决方案 »

  1.   

    select * from (
    select a.id ,a.stationname, a.stationID,b.value1, b.value2 ,b.value3 from  a ,Temp_M9010 b
    where a.id = b.id
    union all
    select a.id ,a.stationname, a.stationID,b.value1, b.value2 ,b.value3 from  a ,Temp_M9011 b
    where a.id = b.id
    .......
    ...
    ..
    .
    )
      

  2.   

    谢谢 yejihui9527
    是我没有说清楚,A表和其他表,是没有外键相关联的,A表和Temp_M9010表有关系的地方就是Temp_M9010表的表名中包含了A表中某一个记录的stationID字段。
      

  3.   

    现在想创建这样一个视图,接结果如下:
    id stationname stationID value1 value2 value3
    1 新货站 M9010 3 3 3
    2 城市站 M9011 4 4 4

    表:Temp_M9010
    id value1 value2 value3
    1 3 3 3
    2 5 5 5
    2 5 5 5的记录怎么不见了呢
      

  4.   

    看看下面这个,希望对你有帮助select  e.id,e.stationname ,e.stationID
    ,(select id from 'Temp_'||e.stationID)
    ,(select value1  from 'Temp_'||e.stationID where rownum=1)
    ,(select value2  from 'Temp_'||e.stationID where rownum=1)
    ,(select value3  from 'Temp_'||e.stationID where rownum=1)
     from a e
      

  5.   

    是这样子的,关联的时候只取Temp_M9010 和 Temp_M9011 的第一条记录
      

  6.   

    创建一个函数,用游标处理A表的每一笔资料对应的stationID,可以得到这样一个字符串:
    str := str || 'select ' || stationID || ' as stationID, value1, value2, value3 from temp_' || stationID || ' where rownum = 1)  union';
    最后一条记录处理完毕后,把最后一个union去掉。然后:
    str := 'create or replace view as select A.id,A.stationname,A.stationID,B.value1,B.value2, B.value3 from A
    left join (' || str ||)' B on A.stationID = B.stationID'
    这就得到了你创建视图的脚本,并把它作为函数的返回值。
    如果你的StationID有增加的时候,再执行一下这个函数就OK了。
      

  7.   

    谢谢各位,根据各位的建议,终于搞定了!
    下面是我的解决方案:
    通过存储过程生成sql语句:
    create or replace procedure transview
    as
      sqla varchar2(30000):='create or replace view Aview as (';
      c_stationId sys_refcursor; --声明游标--
      sqlstr varchar2(20000);
      stationId varchar2(30);
      totalcount number;
      icount number:=0;
      
      outf utl_file.file_type; --声明文件操作对象--
      v_fname varchar2(30);
    begin
       v_fname:='xixuebin.sql'; --文件名称--
       select count(*) into totalcount from A; --得到总记录数--
       open c_stationId for 'select stationId from A';--得到游标--
       outf:=utl_file.fopen('BLOBDIR',v_fname,'w',4000);--打开文件,确保blobdir已经创建--
       utl_file.put_line(outf,sqla); --写入文件--
       loop
       fetch c_stationId into stationId;
       exit when c_stationId%notfound;
       icount:=icount+1;
       sqlstr:='select t.id,t.stationname,t.stationId';
       sqlstr:=sqlstr||'(select s.value1 from Temp_'||stationId||' s where rownum<2) datetime,'||
                        '(select s.value2 from Temp_'||stationId||' s where rownum<2) visibility,'||
                        '(select s.value3 from Temp_'||stationId||' s where rownum<2) temperature';
        sqlstr:=sqlstr||' from A t where t.stationid= '''||stationId||''' ';
        if icount!=totalcount then
        sqlstr:=sqlstr||' union';
        end if;
        utl_file.put_line(outf,sqlstr);
        end loop;
        close c_stationId;
        sqla:=sqla||')';
        utl_file.put_line(outf,')');
        utl_file.fclose(outf);--关闭文件--
        dbms_output.put_line('导出成功');
    end transview;
      

  8.   

    我把生成的sql语句存到xixuebin.sql文件中去了,因为dbms_output.put_line();输出有长度限制,varchar2最大长度也只有30000多,存储不了生成的sql语句,所以只能写到文件中去了,然后执行文件中的sql语句,生成视图