E
E
ellz2019-03-05 07:21:23
PostgreSQL
ellz, 2019-03-05 07:21:23

How to return data from a function as a table?

There is such a function:

CREATE OR REPLACE FUNCTION "public"."GetAllNotes"("long" float8, "lat" float8)
  RETURNS TABLE("userid" int4, "username" varchar, "notename" varchar, "notelong" float8, "notelat" float8) AS $BODY$
  DECLARE 
BEGIN
RETURN query  (SELECT notes."userid", users."name", notes."name", notes."longitude", notes."latitude" FROM notes INNER JOIN users ON notes."userid" = users."id" WHERE  (point(long, lat) <@> point(notes."longitude", notes."latitude") <= 0.124274) );
END
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100
  ROWS 1000

It accepts coordinates as parameters, and displays all records where the distance is less than two hundred meters.
Other tables:

notes:
CREATE TABLE "public"."notes" (
  "id" int4 NOT NULL DEFAULT nextval('notes_id_seq'::regclass),
  "userid" int4 NOT NULL,
  "name" varchar COLLATE "pg_catalog"."default" NOT NULL,
  "longitude" float8 NOT NULL,
  "latitude" float8 NOT NULL,
  CONSTRAINT "notes_pkey" PRIMARY KEY ("id")
)
;
ALTER TABLE "public"."notes" 
  OWNER TO "postgres";

users:
CREATE TABLE "public"."users" (
  "id" int4 NOT NULL DEFAULT nextval('users_id_seq'::regclass),
  "name" varchar COLLATE "pg_catalog"."default" NOT NULL,
  CONSTRAINT "users_pkey" PRIMARY KEY ("id")
)
;

ALTER TABLE "public"."users" 
  OWNER TO "postgres";


And this is how the function returns the table - 5c7df73412a48519309079.png
That is, all all the data in one row
And how to make the table return in this format -
5c7df908c0fbf070738206.png?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
luna3956, 2019-03-05
@ellz

Perhaps you call the function like this select "public"."GetAllNotes", but you need to like thisselect * from "public"."GetAllNotes"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question