I've been looking for a solution to this on other threads however I'm getting just bits and pieces of what I need from other topics which I can't tie together to work correctly.
The background of the problem is this: Each night a transaction log is written to a newly created table in the database, then the primary log is cleared. The name of the table created isn't related to the date (making searching for historical logs difficult - so the table name for a given date can't be guessed or hard coded).
However once the transactions are moved into the new table, a logging table has a new record added to it which stores the dates and other information about the archived records (including the name of the table).
I'm trying to write a function which allows me to pass a date range (start date and end date) into it, then return the name of all tables which have information within the given date range - storing the names in an array. Once the array is populated a SQL query is run against each table in the array to write all of this to a single temporary table, allowing it to be queried.
Some pseudo code would be:
CREATE OR REPLACE FUNCTION transactions(begin_date varchar(10), end_date varchar(10)) RETURN void as $$
Begin
SELECT INTO table_names 'name' from 'log_table' where 'log_date' between begin_date and end_date;
FOREACH table_names LOOP
SQL_Query = "INSERT INTO temporary_table SELECT * from " + table_names;
EXECUTE SQL_Query;
END LOOP;
Any help is appreciated - my logic could be entirely broken on this and I'll need to change my methodology.