1、简单循环示例
do $$
begin
for cnt in 1..10 loop
raise notice 'cnt: %', cnt;
end loop;
end; $$
navicate中执行
2、循环查询
do $$
declare
_record record;
begin
for _record in (SELECT version,description FROM flyway_schema_history ORDER BY installed_rank desc limit 5)
LOOP
raise notice 'cnt: % ,%', _record.version, _record.description;
END LOOP;
END; $$;
3、循环修改多张表
do $$
declare
_record text;
begin
for _record in (SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_name LIKE '%work_manage%')
LOOP
EXECUTE 'ALTER TABLE ' || _record || ' ADD COLUMN soft_bin_result int4;';
EXECUTE 'COMMENT ON COLUMN ' || _record || '.soft_bin_result IS ''软件bin'';';
EXECUTE 'ALTER TABLE ' || _record || ' ADD COLUMN actual_temp float8;';
EXECUTE 'COMMENT ON COLUMN ' || _record || '.actual_temp IS ''实际温度'';';
END LOOP;
END; $$;