You could use a DO
statement (pg 9.0+) with EXECUTE
to loop through all tables:
DO
$$
BEGIN
FOR i IN 1..30 LOOP
EXECUTE '
UPDATE returns.msf_mse a
SET cumret_past_23_' || to_char(i, 'FM00') || ' = b.cumret_past
FROM temp23_' || to_char(i, 'FM00') || ' b
WHERE a.date = b.date
AND a.permno = b.permno';
END LOOP;
END
$$
Pay attention to the leading space when formatting the number with to_char()
. I use the FM
modifier to get rid of it.
Aside: I wouldn't use date
as column name, it easily conflicts with the base type or the function of the same name.