BEGIN
_table_name := 'mytable';
CREATE TEMPORARY TABLE _table_name (
id integer NOT NULL,
name character varying,
active boolean
) ON COMMIT DROP';
-- logic here
RETURN QUERY SELECT table1.id, table1.name FROM _table_name AS table1;
END;
I have simplified my problem. I'm trying to use a variable as the table name.
I know you can do things like SELECT * FROM table1 WHERE id = _id
where _id
is a declared varaible.
I know I can do this EXECUTE
, but then I have a query like:
INSERT INTO table2 (id) SELECT id FROM unnest(_ids) as id
where _ids is an array.
Anyway to solve problem 1 with using a variable as table name? and problem 2, using unnest
inside EXECUTE
?
So the problem is that the queries take _table_name
as a literal table name, it doesn't look like it's using 'mytable' as the table name instead.