10

我想将ID的数组参数输入到Firebird Stored Procedure

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

我需要执行这个 SQL 命令:

SELECT *
FROM CITY
WHERE ID_CITY IN (:INPUT_LIST_ID)

是否可以?谢谢!

4

4 回答 4

10

你也可以使用这样的东西:

SELECT *
FROM CITY
WHERE ID_CITY IN (SELECT ID FROM GetIntegerList('1, 2, 12, 45, 75, 45'))

您必须创建一个名为“GetIntegerList”的新 Firebird 过程,它看起来像这样:

CREATE OR ALTER PROCEDURE "GETINTEGERLIST"("AINTEGERLIST" VARCHAR(32000))
returns (
  ID integer
)
as
  declare variable IntegerList varchar(32000);
  declare variable CommaPos integer;
  declare variable IntegerVal varchar(10);
begin
  IntegerList = AIntegerList || ' ';
  CommaPos = Position(',', IntegerList);

  while (CommaPos > 0) do
  begin
    IntegerVal = Trim(SubString(IntegerList from 1 for CommaPos - 1));

    if (Char_Length(IntegerVal) > 0) then
    begin
      if (IntegerVal similar to '[0-9]*') then
      begin
        ID = Cast(IntegerVal as integer);
        suspend;
      end
    end

    if (Char_Length(IntegerList) > CommaPos) then
      IntegerList = SubString(IntegerList from CommaPos + 1);
    else
      IntegerList = '';

    CommaPos = Position(',', IntegerList);
  end

  IntegerList = Trim(IntegerList);

  if (Char_Length(IntegerList) > 0) then
  begin
    if (IntegerList similar to '[0-9]*') then
    begin
      ID = Cast(IntegerList as integer);
      suspend;
    end
  end
end

请注意,这是在 Firebird 2.5.2 中完成的。

于 2013-08-06T23:36:16.767 回答
5

AFAIK 不,那是不可能的。虽然 Firebird 确​​实有数组数据类型,但对它的支持是基本的,通常不建议使用数组。我认为最简单的解决方案是将数组作为(逗号分隔)字符串传递,然后使用该for execute statement语句来获取结果集,例如

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

然而,这意味着您用来获取结果的语句也会发生变化,而不是WHERE您将使用存储过程的参数CITY

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

发送参数列表的另一个选项是使用全局临时表。这样做的好处是您可以发送大量 ID 而不会超过允许的最大语句大小,但设置呼叫需要更多工作......

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
于 2013-04-05T20:35:19.623 回答
2

如果您使用 Firebird 1.5(它也应该适用于更高版本),您可以使用我制作的这个简单函数将单个字符串转换为整数数组:

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

用法

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

将返回:

INT_VALUE
1
2
3
4
5
200
1
598415
2
于 2016-08-30T13:54:16.417 回答
1

试试这个:

    SELECT *
    FROM CITY
    WHERE '/city1/city2/city.../' containing '/' || ID_CITY || '/';
于 2016-05-14T12:20:06.687 回答