postgresql for循环中的PL/pgSQL“格式错误的数组文字”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38311784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PL/pgSQL "Malformed array literal" error within for loop
提问by ItsTimmy
I have the following pl/pgsql function. (Obviously, this is not the full function, it's just the minimal amount of code needed to reproduce the problem)
我有以下 pl/pgsql 函数。(显然,这不是完整的功能,它只是重现问题所需的最少代码量)
CREATE OR REPLACE FUNCTION test_func(infos TEXT[][])
RETURNS void AS
$$
DECLARE
info TEXT[];
type TEXT[];
name TEXT;
BEGIN
FOREACH info SLICE 1 IN ARRAY infos LOOP
RAISE NOTICE 'Stuff: %', info;
type := info[1];
name := info[2];
RAISE NOTICE 'Done with stuff';
END LOOP;
RETURN;
END;
$$ LANGUAGE plpgsql;
When I run the function using SELECT test_func('{{something, things},{other, data}}'::TEXT[][]);
, I get the following output:
当我使用 运行该函数时SELECT test_func('{{something, things},{other, data}}'::TEXT[][]);
,我得到以下输出:
NOTICE: Stuff: {something,things}
ERROR: malformed array literal: "something"
DETAIL: Array value must start with "{" or dimension information.
CONTEXT: PL/pgSQL function test_func(text[]) line 10 at assignment
I don't understand how this error is happening. When the value of info
is printed, it shows {something,things}
, which seems to me to be a proper array literal.
我不明白这个错误是如何发生的。当info
打印出的值时,它会显示{something,things}
,这在我看来是一个合适的数组字面量。
I am using PostgreSQL version 9.4.7, in case it matters.
我正在使用 PostgreSQL 版本 9.4.7,以防万一。
回答by klin
The variable type
should be text
(not text[]
):
变量type
应该是text
(不是text[]
):
CREATE OR REPLACE FUNCTION test_func(infos TEXT[][])
RETURNS void AS
$$
DECLARE
info TEXT[];
type TEXT;
name TEXT;
...