我正在尝试创建一个 pl/sql 函数(我第一次接触 pl/sql 函数),它将以 10 为基数的数字转换为以 26 为基数的字符串(我的以 26 为基数的是 A..Z)。
create or replace function generateId(numericId IN NUMBER) RETURN VARCHAR2 AS
declare
type array_t is varray(26) of CHAR;
char_array array_t := array_t('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
res varchar2(3);
targetBase INTEGER := char_array.count;
begin
LOOP
res = char_array[REMAINDER(numericId, targetBase)] + result;
numericId = numericId / targetBase;
EXIT WHEN (numericId = 0);
RETURN res;
end;
我得到的错误是:
Error(2,1): PLS-00103: Encountered the symbol "DECLARE" when expecting one of the following: begin function pragma procedure subtype type <an identifier> <a double-quoted delimited-identifier> current cursor delete exists prior external language The symbol "begin" was substituted for "DECLARE" to continue.
我的猜测是我将声明粘贴到了错误的位置,但我不知道它应该放在哪里。
最佳答案
您的代码中有太多语法错误。
oracle 中的赋值是使用 := 而不是 =
数组索引使用 () 而不是 [] 引用
您不能为 IN 变量赋值
numericId = numericId/targetBase
检查 loop statement 的语法- 你缺少结束循环
要获取数组计数,请使用 COUNT()
正如评论所暗示的那样
删除声明
关于oracle - 在 Oracle pl/sql 函数中定义类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31433824/