我需要使用一些oracle内置方法(解析,strlength,...)将字符串转换为数字 我不知道如何为方法内的变量赋值,我不知道在哪里放置声明部分。
CREATE OR REPLACE FUNCTION EXAMPLE (param IN VARCHAR2)
RETURN NUMBER AS
BEGIN
SELECT <string_handling_using_param>
INTO var
FROM DUAL;
RETURN TO_NUMBER(<some computation using var>);
END EXAMPLE ;
我已尝试过围绕此函数的一些变体,比如添加一个OUT参数来存储中间变量,但是我无法从常规SQL中调用该函数...
有关如何实现这一目标的任何建议吗?
答案 0 :(得分:4)
如果我理解正确,你只需要定义“var”变量......
create or replace FUNCTION EXAMPLE (param IN VARCHAR2)
RETURN NUMBER
AS
var VARCHAR2(100); -- This datatype may need modification
BEGIN
select <string_handling>
into var
from dual;
return to_number(<some computation using var>);
END EXAMPLE ;
根据您正在做的事情,可能有一个更好的方法,不需要SELECT ... FROM DUAL:
create or replace FUNCTION EXAMPLE (param IN VARCHAR2)
RETURN NUMBER
AS
var VARCHAR2(100); -- This datatype may need modification
BEGIN
var := <string_handling>;
return to_number(<some computation using var>);
END EXAMPLE ;
答案 1 :(得分:1)
create or replace FUNCTION EXAMPLE (param IN VARCHAR2)
RETURN NUMBER AS
-- declare section between AS and BEGIN
var varchar2(100);
BEGIN
select <string_handling> into var from dual;
-- also note that many built-in functions can be done directly
-- without calling a select, so in many cases
-- var := substr(param,1,10) <or some other string handling>;
-- is perfectly acceptable too.
return to_number(<some computation using var>);
EXCEPTION -- if you need an exception handler
when value_error then
<do something with it or set a default return value or whatever>
END EXAMPLE ;