-- 准备实验环境,创建t01、t02表,并在t01表中插入实验数据
create table t01(num number);
insert into t01 values(1);
insert into t01 values(3);
insert into t01 values(5);
insert into t01 values(7);
insert into t01 values(9);
insert into t01 values(2);
insert into t01 values(4);
insert into t01 values(6);
insert into t01 values(8);
insert into t01 values(10);
commit;create table t02(num number);-- 测试一,不使用PL/SQL,直接查询
select num
from
(
    select num
    from t01
    order by num desc
) tmp01
where rownum<=5;-- 测试二,使用PL/SQL匿名块,将查询结果插入t02表,子查询使用order by
begin
insert into t02(num)
select num
from
(
    select num
    from t01
    order by num desc
) tmp01
where rownum<=5;
end;
/-- 测试三,使用PL/SQL匿名块,将查询结果插入t02表,子查询“不”使用order by
begin
insert into t02(num)
select num
from
(
    select num
    from t01
) tmp01
where rownum<=5;
end;
/

解决方案 »

  1.   

    我的数据库是8.1.5的。上面的测试二总是报错。请大虾指点。SQL> -- 测试二,使用PL/SQL匿名块,将查询结果插入t02表,子查询使用order by
    SQL> begin
      2  insert into t02(num)
      3  select num
      4  from
      5  (
      6      select num
      7      from t01
      8      order by num desc
      9  ) tmp01
     10  where rownum<=5;
     11  end;
     12  /
        order by num desc
        *
    ERROR 位于第 8 行:
    ORA-06550: 第 8 行, 第 5 列:
    PLS-00103: Encountered the symbol "ORDER" when expecting one of the following:
    . ) , @ with <an identifier>
    <a double-quoted delimited-identifier> group having intersect
    minus partition start union where connect
    已用时间:  00: 00: 00.02
    SQL>另外我还有一帖,问同样类型的问题。
    http://community.csdn.net/Expert/topic/4404/4404955.xml?temp=.4963953
      

  2.   

    在Oracle 8i以前的版本中(如8.0.5),子查询
    中不支持ORDER by子句,
      

  3.   

    把order by去掉,当然可以正确执行。
    可是得到的结不是我想要的,我要的数据是要排过序的啊。哪位能在新版的oracle(9i 或10g)上帮我试一下,告诉我结果。先谢过了。难道不成真的升级数据库啊。
      

  4.   

    建个临时表保存order by 后的数据
      

  5.   

    Oracle8.1.5真的是太低了,至少應該升到Oracle9i呀,不然有相當多的功能和語法無法在Oracle8版本中使用的!!!