我怎么能传递一个"阵"值到我的存储过程?
我希望能够传递一个值的数组我的存储过程,而不是调用价值的过程连续。
I want to be able to pass an "array" of values to my stored procedure, instead of calling "Add value" procedure serially.
任何人都可以提出一个办法做到这一点?我失去了一些东西在这里?
Can anyone suggest a way to do it? am I missing something here?
编辑:我将使用PostgreSQL / MySQL的,我还没有决定。
I will be using PostgreSQL / MySQL, I haven't decided yet.
由于克里斯指出,在PostgreSQL里是没有问题的 - 任何基本类型(如int,文本)有它自己的阵列亚型,您还可以创建自定义类型,包括复合的。例如:
As Chris pointed, in PostgreSQL it's no problem - any base type (like int, text) has it's own array subtype, and you can also create custom types including composite ones. For example:
CREATE TYPE test as (
n int4,
m int4
);
现在,你可以轻松地创建测试数组:
Now you can easily create array of test:
select ARRAY[
row(1,2)::test,
row(3,4)::test,
row(5,6)::test
];
您可以编写将成倍N * M在阵列中的每个项目的功能,并返回乘积之和:
You can write a function that will multiply n*m for each item in array, and return sum of products:
CREATE OR REPLACE FUNCTION test_test(IN work_array test[]) RETURNS INT4 as $$
DECLARE
i INT4;
result INT4 := 0;
BEGIN
FOR i IN SELECT generate_subscripts( work_array, 1 ) LOOP
result := result + work_array[i].n * work_array[i].m;
END LOOP;
RETURN result;
END;
$$ language plpgsql;
和运行它:
# SELECT test_test(
ARRAY[
row(1, 2)::test,
row(3,4)::test,
row(5,6)::test
]
);
test_test
-----------
44
(1 row)