如何通过自定义类型多dimmensional阵列PostgreSQL中函数参数? [英] How to pass custom typed multi-dimmensional array as a function parameter in PostgreSQL?

查看:221
本文介绍了如何通过自定义类型多dimmensional阵列PostgreSQL中函数参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个简单的自定义类型:

I've made a simple custom type:

CREATE TYPE public."FriendDetails" AS
   ("Email" character varying,
    "Name" character varying);
ALTER TYPE public."FriendDetails"
  OWNER TO postgres;

要在以下功能中使用(这只是返回一个字符串 - 联系人!):

To be used in the following function (It just returns a string - contact!):

CREATE OR REPLACE FUNCTION public."addFriend"(
    "GroupName" character varying,
    friends "FriendDetails"[])
  RETURNS character varying AS
$BODY$BEGIN
    RETURN "contact!";
END;$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION public."addFriend"(character varying, "FriendDetails"[])
  OWNER TO postgres;

使用pgAdminIII生成上述code。

The above code was generated using pgAdminIII.

我试着查询,如下所示:

I tried to query it as follows:

select addFriend('Champs',ARRAY[['qwe','asd'],['zxc','rty']]::public."FriendDetails"[]);

和得到了一个错误:

ERROR:  malformed record literal: "qwe"
LINE 1: select addFriend('Champs',ARRAY[['qwe','asd'],['zxc','rty']]...
                                         ^
DETAIL:  Missing left parenthesis.
********** Error **********

ERROR: malformed record literal: "qwe"
SQL state: 22P02
Detail: Missing left parenthesis.
Character: 34

我试图取代花括号({}),而失去了 ARRAY ,但似乎没有任何工作 - 他们会导致语法错误。

I tried substituting curly braces ({}), and losing the ARRAY, but nothing seems to work - they'll resulted in syntax errors.

能否有人好心显示语法应该如何使用(1)大括号和(2)使用 ARRAY 构造函数调用此方法。

Can someone kindly show how the syntax should be to invoke this method using the (1) curly braces and (2) by using the ARRAY constructor.

推荐答案

有在code错误:

使用圆括号的行/记录类型的常量的:

ARRAY[('qwe','asd'),('zxc','rty')]

添加到双引号函数名称:

Add doublequotes to function name:

select "addFriend"('Champs',ARRAY[('qwe','asd'),('zxc','rty')]::public."FriendDetails"[]);

对于字符串常量使用singlequotes:

Use singlequotes for string constants:

RETURN 'contact!';

这篇关于如何通过自定义类型多dimmensional阵列PostgreSQL中函数参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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