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

后端 未结 4 1511
忘了有多久
忘了有多久 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 12:49

    AFAIK no, thats not possible. While Firebird does have array data type, support for it is rudimentary and use of arrays is generally not recommended. I think the easiest solution would be to pass the array as (comma separated) string and then use the for execute statement statement to get the resultset, something like

    create procedure CITY (INPUT_LIST_ID varchar(1024)) 
    returns( ... )
    as
    begin
      for execute statement
        'select ... from T where ID_CITY IN ('|| INPUT_LIST_ID ||')' into ...
      do begin
         suspend;
      end
    end
    

    This means however that the statement you use to get the result also changes, instead of WHERE you would use the parameter of the stored procedure CITY:

    SELECT * FROM CITY('1, 2, 12, 45, 75, 45')
    

    Another option to send the parameter list is to use global temporary table. This has the pro that you can send huge number of IDs without exceeding the maximum allowed statements size but it is more work to set up the call...

    create global temporary table SP_CITY_PARAMS (
      id int not null primary key
    )
    on commit delete rows;
    
    create procedure CITY
    returns( ... )
    as
    begin
      for select ... from T where ID_CITY IN (
          select id from SP_CITY_PARAMS
      ) into ...
      do begin
         suspend;
      end
    end
    

提交回复
热议问题