1

-- I can not understand where the error

CREATE OR REPLACE FUNCTION get_person_membership (IN person_urn CHARACTER VARYING)

   RETURNS TEXT  AS
$BODY$

DECLARE
result text;
urn ALIAS FOR $1;

BEGIN

SELECT INTO result pers.mx_groupmember FROM mt_person AS pers, mxt_recordheader AS rech
WHERE rech.primaryurn = 'urn'
AND rech.entitytype = 'person' 
AND rech.logicalserverprefix = 'EA'
AND rech.id = pers.id;
RETURN result;

END;
$BODY$
LANGUAGE plpgsql
VOLATILE
COST 100
4
  • 1
    change SELECT INTO result pers.mx_groupmember to SELECT pers.mx_groupmember into result Commented Mar 28, 2017 at 13:44
  • also, you don't actually use the person_urn parameter in your query (neither its urn alias, which is completely unnecessary, because the parameter has a name already). Commented Mar 28, 2017 at 13:48
  • 1
    I suppose you should write rech.primaryurn =urn instead of rech.primaryurn ='urn' Commented Mar 28, 2017 at 13:49
  • Thank's a lot Vao Commented Mar 29, 2017 at 15:06

2 Answers 2

1

i simplified your query:

  • For input you can use $1, this goes to direct your condition.

  • You can return direct your result if you dont need anywhere else.

    select * from get_person_membership('something');


CREATE OR REPLACE FUNCTION get_person_membership (IN person_urn CHARACTER VARYING) 
RETURNS TEXT  AS
$BODY$

BEGIN

RETURN (select pers.mx_groupmember --returns single value
FROM mt_person AS pers, mxt_recordheader AS rech
WHERE rech.primaryurn = $1 --input value from person_urn 
AND rech.entitytype = 'person' 
AND rech.logicalserverprefix = 'EA'
AND rech.id = pers.id);

END;
$BODY$
LANGUAGE plpgsql
VOLATILE
COST 100
Sign up to request clarification or add additional context in comments.

6 Comments

return from? (@_@)
same as return (select pers.mx_groupmember FROM mt_person AS pers, mxt_recordheader AS rech WHERE rech.primaryurn = $1 --input value from person_urn AND rech.entitytype = 'person' AND rech.logicalserverprefix = 'EA' AND rech.id = pers.id) related link: first solution
I have never heard of RETURN FROM either. And don't found any evidence that it's supposed to work. But apparently, it does (with 9.5 at least).
I tested in 9.6 and indeed it works. I wonder why isn't it in the docs.
This is probably something like the assignment with = see 9.3 vs. 9.4: it was supported a long ago (before 9.3), but was considered a hidden feature (and a bad design, because it can be easily confused with the equality test). This syntax seems to conform (at least) to PERFORM, so it might eventually get added to the docs. But until then, this is an undocumented feature, and as such, it might change without prior notice. Use this syntax very carefully.
|
0

No need for PL/pgSQL, a simple SQL function will do:

CREATE OR REPLACE FUNCTION get_person_membership (IN person_urn CHARACTER VARYING)
   RETURNS TEXT  AS
$BODY$
  SELECT pers.mx_groupmember 
  FROM mt_person AS pers
    JOIN mxt_recordheader AS rech ON rech.id = pers.id
  WHERE rech.primaryurn = person_urn --<< input parameter
    AND rech.entitytype = 'person' 
    AND rech.logicalserverprefix = 'EA';
$BODY$
LANGUAGE sql;

Comments

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.