3

我尝试创建一个与 Oracle 中的函数具有相同功能的TRANSLATE函数,我在 Oracle 中创建了函数并且效果很好,但是当我尝试将此代码重写为 Firebird 时出现问题。我收到“不支持表达式评估”的错误。为什么?

CREATE FUNCTION TRANSLATEE
(text varchar(10000),
toReplace varchar(10000),
replacePattern varchar(10000)
)
RETURNs VARCHAR(100)
aS

declare variable cut varchar(100);
declare variable i integer;
declare variable position1  varchar(100);
declare variable resultat  varchar(100);
declare variable letter  varchar(100);
declare variable lenght integer;
BEGIN
i = 1;
resultat ='';
lenght =  char_length(:text);
while(i<lenght) do
begin
    cut = substring(:text from i for 1);
    position1 = position(:toReplace , cut);
      if (position(:toReplace , cut) >0) then
      begin
        letter = substring(:replacePattern from position1 for 1);
        resultat = resultat||''||letter;
        end
      else 
      begin
       resultat = resultat ||''|| cut;
      end
  i = i+1;
end
  RETURN resultat;
END;
4

2 回答 2

1

主要问题是您已声明position1为 aVARCHAR(100)而不是INTEGER. 这会导致此处出现无用的错误,因为没有采用 varchar 参数的子字符串版本(在此上下文中,不支持从 varchar 到整数的转换)。

您的函数的工作(或至少:编译)版本是:

CREATE OR ALTER FUNCTION TRANSLATE_func
   (text varchar(10000), 
    toReplace varchar(10000), 
    replacePattern varchar(10000))
RETURNS VARCHAR(100)
AS
   declare variable resultat varchar(100);
   declare variable cut varchar(100);
   declare variable i integer;
   declare variable position1  integer;
   declare variable letter  varchar(100);
   declare variable lenght integer;
BEGIN
   i = 1;
   resultat ='';
   lenght =  char_length(text);
   while(i <= lenght) do
   begin
       cut = substring(text from i for 1);
       position1 = position(cut, toReplace);
       if (position1 > 0) then
       begin
           letter = substring(replacePattern from position1 for 1);
           resultat = resultat||''||letter;
       end
       else 
       begin
           resultat = resultat ||''|| cut;
       end
       i = i+1;
   end
   return resultat;
END
于 2015-04-11T09:26:31.597 回答
0

对于火鸟 3

SET TERM ^ ;

create function translator (
    inp varchar(10000),
    pat varchar(1000),
    rep varchar(1000))
returns varchar(10000)
as
    declare variable tex varchar(10000);
    declare variable inp_idx integer = 1;
    declare variable cha char(1);
    declare variable pos integer;
begin
  tex = '';
  while (inp_idx <= char_length(inp)) do
  begin
    cha = substring(inp from inp_idx for 1);
    pos = position(cha, pat);
    if (pos > 0) then
      cha = substring(rep from pos for 1);
    tex = tex || cha;
    inp_idx = inp_idx + 1;
  end
  return tex;
end^

SET TERM ; ^

测试

select translator('džiná lasaí ireo dana kýrne číraž', 'ážíýč', 'AZIYC')
  from rdb$database;

结果

dZinA lasaI ireo dana kYrne CIraZ
于 2019-12-03T10:42:23.333 回答