Look in the plpgsql docs on EXECUTE. But for something that simple, why don't you just generate a query ?