帮忙把这段MSSQL的代码改成ORACLE的,谢谢。

人鱼传说 2011-09-14 09:35:05
昨天在论坛提了个问,有位朋友写了如下代码,今天想把这段弄成ORACLE的,弄不出来呀。。。。来求助一下。。


create table tb(工艺序号 int,投入量 int,转换率 int)
insert into tb select 1,20,1
insert into tb select 2, 0,2
insert into tb select 3, 0,1
insert into tb select 4,10,2
insert into tb select 5, 0,3
insert into tb select 6, 5,2
go
;with cte as(
select *,投入量*转换率 as 应产出量 from tb where 工艺序号=1
union all
select b.*,(b.投入量+a.应产出量)*b.转换率 from cte a inner join tb b on a.工艺序号=b.工艺序号-1
)
select * from cte
/*
工艺序号 投入量 转换率 应产出量
----------- ----------- ----------- -----------
1 20 1 20
2 0 2 40
3 0 1 40
4 10 2 100
5 0 3 300
6 5 2 610

(6 行受影响)

*/
go
drop table tb
...全文
90 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
cutebear2008 2011-09-16
  • 打赏
  • 举报
回复
oracle的sql中就不支持用with(cte)来替代connect by的递归查询。
所以你这样写
union all
select b.*,(b.投入量+a.应产出量)*b.转换率 from cte a inner join tb b on a.工艺序号=b.工艺序号-1
是不对的,这里from cte就是递归查询了,所以LZ你可以搜索下oracle connect by递归查询。

[Quote=引用 1 楼 hao1hao2hao3 的回复:]

其实可以自己试着修改一下,语法差不多吧!

SQL code

create table tb(工艺序号 int,投入量 int,转换率 int)
insert into tb select 1,20,1
insert into tb select 2, 0,2
insert into tb select 3, 0,1
insert into tb select 4,10,2
insert……
[/Quote]
cutebear2008 2011-09-16
  • 打赏
  • 举报
回复
这个是cte的递归调用,你搜索下oracle的通用表达式用法!
[Quote=引用 4 楼 baronyang 的回复:]

求解决。。。。。。
[/Quote]
人鱼传说 2011-09-15
  • 打赏
  • 举报
回复
求解决。。。。。。
chcchb 2011-09-15
  • 打赏
  • 举报
回复
临时表分两种,根据SESSION和TRANSACTION分。
CREATE GLOBAL TEMPORARY TABLE table_name ON COMMIT PRESERVE ROWS.
在SESSION结束是自动删除。
CREATE GLOBAL TEMPORARY TABLE table_name ON COMMIT DELETE ROWS
在提交COMMIT命令时候自动删除。

也是从其他地方找来的。。
人鱼传说 2011-09-14
  • 打赏
  • 举报
回复
不是这里的错误。。。
是在union all下面的select里引用了with 的cte临时表。。
oralce里提示非法引用。。。。

union all
select b.*,(b.投入量+a.应产出量)*b.转换率 from cte a inner join tb b on a.工艺序号=b.工艺序号-1
--是这句错了。。。不知道怎么改。。。
hao1hao2hao3 2011-09-14
  • 打赏
  • 举报
回复
其实可以自己试着修改一下,语法差不多吧!


create table tb(工艺序号 int,投入量 int,转换率 int)
insert into tb select 1,20,1
insert into tb select 2, 0,2
insert into tb select 3, 0,1
insert into tb select 4,10,2
insert into tb select 5, 0,3
insert into tb select 6, 5,2
go
;with cte as(
select tb.*,投入量*转换率 应产出量 from tb where 工艺序号=1
union all
select b.*,(b.投入量+a.应产出量)*b.转换率 from cte a inner join tb b on a.工艺序号=b.工艺序号-1
)
select * from cte
go
drop table tb




1、* 要指定表前缀。
2、去掉列别名的as

试试看看行不行,有错误提示再说。

17,086

社区成员

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

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