funcion de normalizacion de secuencia.txt 671 B

12345678910111213141516171819
  1. CREATE OR REPLACE FUNCTION normalize_seq() RETURNS VOID AS $$
  2. DECLARE
  3. statements CURSOR FOR
  4. SELECT '''' || relname || '''' as seqname,substring(relname from 1 for (char_length(relname) -7)) as tablename
  5. FROM pg_class
  6. WHERE relkind = 'S' and
  7. relname in
  8. (select table_name||'_id_seq'
  9. from information_schema.columns
  10. where table_catalog = 'golden_24_10' and table_schema = 'public' and column_name = 'id');
  11. BEGIN
  12. FOR smt IN statements LOOP
  13. RAISE NOTICE 'Secuencia actualizada: %',smt.seqname;
  14. EXECUTE 'select pg_catalog.setval(' || smt.seqname || ', (select max(id)+1 from ' || smt.tablename || '), true)';
  15. END LOOP;
  16. END;
  17. $$ LANGUAGE PLPGSQL;
  18. SELECT normalize_seq();