[英]Calculate Single Column in Oracle
我有如下表:
id jancuk pijut
-------------------------------
1 20 * 4
2 2 + 4
3 3 - 1 + 2
4 20 / 5
我想更新列pijut
从结果jancuk
列。 在oracle中使用查询是否可能?
因此,如果表已更新,则该表应如下所示:
id jancuk pijut
-------------------------------
1 20 * 4 80
2 2 + 4 6
3 3 - 1 + 2 4
4 20 / 5 4
请帮帮我。 谢谢。
您可以将函数与动态SQL结合使用-
create or replace function calc_value(str in varchar2) return number is
val_ number;
begin
if str is null then
return null;
end if;
execute immediate 'begin :val_ := '||str||'; end;' using out val_;
return val_;
end;
/
结果选择将是-
with t as
(
select 1 as id, ' 20 * 4 ' as jancuk from dual union all
select 2 as id, ' 2 + 4 ' as jancuk from dual union all
select 3 as id, ' 3 - 1 + 2 ' as jancuk from dual union all
select 4 as id, ' 20 / 5 ' as jancuk from dual
)
select id, jancuk, calc_value(jancuk) as pijut
from t
尝试:
update tbl
set "pijut" = xmlquery(replace( "jancuk", '/', ' div ' ) returning content ).getNumberVal();
有关更多信息链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.