我怎么能传递一个"阵"值到我的存储过程? [英] How can I pass an "array" of values to my stored procedure?

查看:127
本文介绍了我怎么能传递一个"阵"值到我的存储过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够传递一个值的数组我的存储过程,而不是调用价值的过程连续。

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)

这篇关于我怎么能传递一个"阵"值到我的存储过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆