How to input an array parameter of values to Firebird Stored Procedure?

后端 未结 4 1514
忘了有多久
忘了有多久 2021-02-08 12:23

I would like to input an array parameter of IDs to Firebird Stored Procedure.

:INPUT_LIST_ID = [1, 2, 12, 45, 75, 45]

I\'m need

4条回答
  •  生来不讨喜
    2021-02-08 13:10

    If you use Firebird 1.5 (It should work on higher versions too), you can use this simple function i made to convert a single string into an integer array:

    create or alter procedure INTEGER_LIST (
        input varchar(4096))
    returns (
        INT_VALUE integer)
    as
    declare variable CHAR_COUNT integer;
    declare variable PARAM_LENGTH integer;
    declare variable READ_VALUE char(1);
    declare variable CURRENT_INTEGER varchar(20);
    begin
        param_length = strlen(input);
        char_count = 0;
        current_integer = '';
        while (char_count < param_length) do begin
            char_count = :char_count + 1;
            read_value = substr(:input, :char_count, :char_count);
            if (:read_value <> ',') then begin
                current_integer = :current_integer || :read_value;
            end else if (:read_value <> ' ') then  begin
                int_value = cast(:current_integer as integer);
                current_integer = '';
                suspend;
            end
    
            if (:char_count = :param_length) then begin
                int_value = cast(:current_integer as integer);
                suspend;
            end
        end
    end
    

    Usage

    select int_value from integer_list('1,2,3,4, 5, 200, 1, 598415, 2')

    Will return this:

    INT_VALUE
    1
    2
    3
    4
    5
    200
    1
    598415
    2
    

提交回复
热议问题