我想将数组参数输入 Firebird存储过程。
:INPUT_LIST_ID = [1,2,12,45,75,45]
我需要执行这个SQL命令:
SELECT *
FROM CITY
WHERE ID_CITY IN (:INPUT_LIST_ID)
有可能吗? 谢谢!
答案 0 :(得分: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中完成的。
答案 1 :(得分:4)
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')
发送参数列表的另一个选项是使用global temporary table。有专家,您可以发送大量的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
答案 2 :(得分:1)
试试这个:
SELECT *
FROM CITY
WHERE '/city1/city2/city.../' containing '/' || ID_CITY || '/';
答案 3 :(得分:1)
如果您使用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