救急:查询正确,但无法把查询结果插入另一张表???

icbm 2005-12-16 09:33:50
-- 准备实验环境,创建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;
/
...全文
193 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
shouym 2005-12-19
  • 打赏
  • 举报
回复
建个视图不就行了
zzwind5 2005-12-19
  • 打赏
  • 举报
回复
建个临时表保存order by 后的数据
软侠 2005-12-19
  • 打赏
  • 举报
回复
Oracle8.1.5真的是太低了,至少應該升到Oracle9i呀,不然有相當多的功能和語法無法在Oracle8版本中使用的!!!
ab5669 2005-12-17
  • 打赏
  • 举报
回复
在Oracle 9i下测试通过
icbm 2005-12-17
  • 打赏
  • 举报
回复
把order by去掉,当然可以正确执行。
可是得到的结不是我想要的,我要的数据是要排过序的啊。

哪位能在新版的oracle(9i 或10g)上帮我试一下,告诉我结果。先谢过了。

难道不成真的升级数据库啊。
lion_fenn 2005-12-17
  • 打赏
  • 举报
回复
把order by 去掉
hevin 2005-12-16
  • 打赏
  • 举报
回复
在Oracle 8i以前的版本中(如8.0.5),子查询
中不支持ORDER by子句,
icbm 2005-12-16
  • 打赏
  • 举报
回复
我的数据库是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

17,140

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧