PostgreSQL:子查询必须返回一列

时间:2016-11-01 12:34:23

标签: postgresql plpgsql

看起来很明显的错误,仍然没有机会找到它。我已经在本函数中本地化了错误:

  SELECT GROSS, VAT_RATE , (GROSS * VAT_RATE) AS VAT , (GROSS + VAT) AS NET
    FROM ....

使用此类型:

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;

我在运行时遇到这个错误:

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
);

2 个答案:

答案 0 :(得分:3)

我会摆脱所有的复杂性并使其成为简单的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;

答案 1 :(得分:1)

我的类型

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

我做

sRow map.get_near_link;

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