Hola, una vez tuve que hacer algo similar de agregar el mismo campo a muchas tablas y lo resolví con una función que me cree, donde le pasaba el esquema, el nombre del atributo y el tipo de dato, recorría mis tablas y bum 'add column':

select agregar_atributo('esquema', 'nuevo', 'timestamp');


CREATE OR REPLACE FUNCTION agregar_atributo(esquema text, atributo text, tipo text)
  RETURNS integer AS
$BODY$
     DECLARE
     tabla text;
     comando text;

     cantidad int :=0;
     flag boolean:=true;
     BEGIN
     FOR tabla IN SELECT ns.nspname||'.'|| pg.relname as tb FROM pg_class pg join pg_namespace ns  on (pg.relnamespace = ns.oid)
                where pg.relkind='r' and ns.nspname=$1   LOOP
     comando:='ALTER TABLE  IF EXISTS '|| tabla ||' add  COLUMN '|| atributo ||' '|| tipo || ' ;' ;

     BEGIN
       RAISE NOTICE 'Ejecutando  % ', comando;
       EXECUTE comando;
           EXCEPTION

           WHEN OTHERS THEN

           RAISE notice 'Error en tabla %', tabla;
               flag:=false;


               END;
      if flag=true then
       cantidad:=cantidad+1;
      end if;
    flag:=true;

    END LOOP;

    return cantidad;
     END;
     $BODY$
  LANGUAGE plpgsql ;


On 22/10/17 09:53, jvenegasperu . wrote:
Buen día todos
Hoy se me presento este caso que necesito agregar un campo a todas las tablas son como 300 tablas así que supongo habra alguna forma con algún script alguien que me pueda ayudar con esto


Reply via email to