0

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;

The above code was generated using pgAdminIII.

I tried to query it as follows:

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

And got an error:

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

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

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.

1

1 Answer 1

1

There are errors in your code:

Use round brackets for row/record types constants:

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

Add doublequotes to function name:

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

Use singlequotes for string constants:

RETURN 'contact!';
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. This has been a real help. I also understand we can write the same without the ARRAY syntax. Using only the {}. Could you show me how?
@Vineet select "addFriend"('Champs','{"(qwe,asd)","(zxc,rty)"}'); - the simplest way.
Thank you once again. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.