+ -
当前位置:首页 → 问答吧 → 帮忙把这段MSSQL的代码改成ORACLE的,谢谢。

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

时间:2011-09-14

来源:互联网

昨天在论坛提了个问,有位朋友写了如下代码,今天想把这段弄成ORACLE的,弄不出来呀。。。。来求助一下。。

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 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

作者: baronyang   发布时间: 2011-09-14

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

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 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

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

作者: hao1hao2hao3   发布时间: 2011-09-14

不是这里的错误。。。
是在union all下面的select里引用了with 的cte临时表。。
oralce里提示非法引用。。。。
SQL code

union all
select b.*,(b.投入量+a.应产出量)*b.转换率 from cte a inner join tb b on a.工艺序号=b.工艺序号-1
--是这句错了。。。不知道怎么改。。。

作者: baronyang   发布时间: 2011-09-14

临时表分两种,根据SESSION和TRANSACTION分。
CREATE GLOBAL TEMPORARY TABLE table_name ON COMMIT PRESERVE ROWS.
在SESSION结束是自动删除。
CREATE GLOBAL TEMPORARY TABLE table_name ON COMMIT DELETE ROWS
在提交COMMIT命令时候自动删除。

也是从其他地方找来的。。

作者: chcchb   发布时间: 2011-09-15