在 Oracle pl/sql 函数中定义类型

And*_*rey 2 oracle plsql oracle-sqldeveloper

我正在尝试创建一个 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;
Run Code Online (Sandbox Code Playgroud)

我收到的错误是:

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. 
Run Code Online (Sandbox Code Playgroud)

我的猜测是我将声明粘贴到了错误的位置,但我不知道它应该放在哪里。

6to*_*ton 5

您的代码中有太多语法错误。

  1. oracle 中的赋值是使用 := 而不是 = 完成的

  2. 数组索引使用 () 而不是 [] 引用

  3. 不能给 IN 变量赋值numericId = numericId / targetBase

  4. 检查循环语句的语法- 您缺少结束循环

  5. 要获取数组计数,请使用 COUNT()

    正如评论所暗示的那样

  6. 删除声明