Can anyone help me with this? I have a task to write a function, which would generate HTML tables from given table name in PostgreSQL(plpgsql language).
I have written this, but it’s far from what I need. It would generate a table for columns I would give (at the moment just one), but I need to just give the table a name.
CREATE OR REPLACE FUNCTION genhtml2(tablename text, columnname text)
RETURNS text AS
$BODY$
DECLARE
result text := ”;
searchsql text := ”;
var_match text := ”; BEGIN
searchsql := ‘SELECT ‘ || columnname || ‘
FROM ‘ || tablename || ”;result := '<table>'; FOR var_match IN EXECUTE(searchsql) LOOP IF result > '' THEN result := result || '<tr>' || var_match || '</tr>'; END IF; END LOOP; result := result || '</table>';RETURN result; END; $BODY$ LANGUAGE ‘plpgsql’ IMMUTABLE;
You could search the calalogs for the columns in the table first, then use them to generate the query and to set the table header.
Obviously this gets messy and brittle fast…