Union blows up because of the JSON column, and apparently, temp tables are almost useless inside PostgreSQL functions.
I'm coming from the MS SQL Server world, and it seems that everything I try in PostgreSQL is wrong. This is what I am trying to do. Can someone please tell me the best way to do it? Or any way to do it for that matter?
create or replace function getNextStories( previous_id bigint )
returns setof output_stories as
$$
create temp table output_stories (
"id" bigint,
submitted_date timestamp with time zone,
author character varying,
title character varying,
"content" json,
pinned boolean
);
insert into output_stories("id", submitted_date, author, title, "content", pinned)
select "id", submitted_date, categories, author, title, "content", pinned
from Story
where Pinned = true;
insert into output_stories("id", submitted_date, author, title, "content", pinned)
select "id", submitted_date, categories, author, "content", title, pinned
from Story
where Pinned = false
and "id" < previous_id
order by "id" desc
limit 20;
select * from output_stories
$$
language 'sql' ;
I have tried many different things and every way I go I get an error. This particular attempt returns this.
ERROR: type "output_stories" does not exist
********** Error **********
ERROR: type "output_stories" does not exist
SQL state: 42704
UNION ALL?