0

Looking obvious error, still see no chance to find it. I've made to localize error in this function:

CREATE OR REPLACE FUNCTION findRecipientsByQuestion(questionId BIGINT)
RETURNS SETOF BIGINT AS $$
DECLARE
  question questionObject;  

  BEGIN
    question := (
      SELECT "a"."id", "a"."body", "a"."author_id", "a"."category_id", "a"."urgent", "a"."created_at", "a"."locale_id", "a"."lat", "a"."lng", "a"."radius" 
      FROM "question" "a" 
      WHERE "a"."id"=questionId 
      LIMIT 1
    );


    RETURN QUERY SELECT "a"."id" 
    FROM "user" "a" INNER JOIN "notifications" "b" ON ("a"."id"="b"."user_id")
    WHERE ("b"."category_id"=question.category_id OR "b"."urgent") AND 
        isGeoMatch("a"."lat", "a"."lng", "a"."radius", question.lat, question.lng, question.radius);
  END 
  $$LANGUAGE plpgsql;

Which uses this type:

CREATE TYPE questionObject AS (
  id BIGINT,
  body VARCHAR,
  author_id BIGINT,
  category_id BIGINT,
  urgent BOOLEAN,
  created_at TIMESTAMP,
  locale_id BIGINT,
  lat DOUBLE PRECISION,
  lng DOUBLE PRECISION,
  radius INTEGER
);

And I'm getting this error in runtime:

Error: subquery must return only one column
6
  • You have, in fact, two queries. Did you try each separately? Which one is triggering the error? Commented Nov 1, 2016 at 12:41
  • good notice let me try Commented Nov 1, 2016 at 12:42
  • well, problem is in the first query.... Commented Nov 1, 2016 at 12:44
  • 1
    You either select into as in @Juan answer or do question := (select ("a"."id", ...) from ...) Commented Nov 1, 2016 at 12:48
  • 1
    btw no need to use so many "" unless you have uppercase and spaces in the variables name. So suggestion dont use uppercase or spaces to keep code simple. Commented Nov 1, 2016 at 12:49

2 Answers 2

3

I would just get rid off all the complexity and make it plain sql:

create or replace function findrecipientsbyquestion (
    _questionid bigint
) returns setof bigint as $$

    select a.id 
    from
        user a
        inner join
        notifications b on a.id = b.user_id
        inner join (
            select categoty_id, lat, lng, radius 
            from question
            where id = _questionid 
            limit 1
        ) q on q.category_id = b.category_id or b.urgent
    where isgeomatch(a.lat, a.lng, a.radius, q.lat, q.lng, q.radius);

$$ language sql;
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for suggestion, this definitely looks cleaner than my sollution
1

with my type

CREATE TYPE map.get_near_link AS
   (link_id integer,
    distance integer,
    direction integer,
    geom public.geometry(4));

I do

sRow map.get_near_link;

SELECT i.Link_ID, i.Distance, i.Direction, i.geom into sRow 
FROM 
    index_query i;

1 Comment

Thanks, this did the job ;) And another thanks for the "" advice.

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.