0

如何将此功能转移到火鸟

create function `candidat`(in_num   decimal(10,2),
                           in_group integer unsigned)   
       returns integer unsigned 
       deterministic   
       language sql 
begin   
   return case in_group when 1 then floor(in_num / 3.0 + 0.99)
                        when 2 then floor(in_num / 3.0 + 0.50)
                               else floor(in_num / 3.0) end; 
end
4

2 回答 2

1

有 3 种方法可以提取您想要的数据:

  1. 在您的 SQL 中添加一个 CASE:

    select
       case when :in_group = 1 then floor(:in_num / 3.0 + 0.99)
            when :in_group = 2 then floor(:in_num / 3.0 + 0.50)
            else floor(:in_num / 3.0) end
    from
       RDB$DATABASE
    

    OBS:RDB$DATABASE它只是一个表示例de结果,并且“:in_num”和“:in_group”它是一个变量/参数,您可以使用一个表的列进行更改,或者只注入您需要的值

  2. 创建一个 PROCEDURE,如:

    SET TERM ^ ;
    
    CREATE OR ALTER procedure candidat (in_num decimal(10,2), in_group integer)
       returns ( calculed_value integer )
    as
    begin
      if (:in_group = 1) then
      begin
        calculed_value = floor(:in_num / 3.0 + 0.99);
      end
      else if (:in_group = 2) then
      begin
        calculed_value = floor(:in_num / 3.0 + 0.50);
      end
      else
      begin
        calculed_value = floor(:in_num / 3.0);
      end
    
      suspend;
    end
    ^
    
    SET TERM ; ^
    

    你可以像这样使用它:

    select 
       CALCULED_VALUE 
    from 
       candidat( 100, 2)
    
  3. 您可以使用 C++ 之类的语言创建一个库 (UDF),该语言使用该函数生成一个.dllou .so(for linux),candidat然后将其添加到数据库中。

    为此,您可以查看https://www.firebirdsql.org/en/writing-udfs-for-interbase/上的文档

    然后你可以使用像这样的UDF:

    select
      candidat(1,3)
    from
      <TABLE>
    
于 2018-10-05T11:01:47.217 回答
0

您可以创建 Firebird 3 PSQL函数,它几乎与 MySQL 函数相同。市长的区别只是创建语法:

create function candidat(in_num   decimal(10,2),
                         in_group integer)   
       returns integer
as
begin   
   return case in_group when 1 then floor(in_num / 3.0 + 0.99)
                        when 2 then floor(in_num / 3.0 + 0.50)
                               else floor(in_num / 3.0) end; 
end

由于 Firebird 没有无符号整数,因此您需要使用普通(有符号)integer。给定足够的输入,否则考虑切换到bigint.

于 2018-10-06T08:42:23.907 回答