PostgreSQL
 sql >> Database >  >> RDS >> PostgreSQL

Accesso alla tabella dinamica PostgreSQL

OK, ho trovato una soluzione:

CREATE OR REPLACE FUNCTION getProductById(cid int) RETURNS RECORD AS $$
    DECLARE
    result RECORD;

    BEGIN
        EXECUTE 'SELECT * FROM ' || (SELECT ('products.' || (select category_name from category where category_id = cid) || '_view')::regclass) INTO result;

        RETURN result;
    END;
$$ LANGUAGE plpgsql;

e per selezionare:

SELECT * FROM getProductById(7) AS b (category_id int, ... );

funziona per PostgreSQL 9.x