From: | Anthony Sotolongo <asotolongo(at)gmail(dot)com> |
---|---|
To: | "jvenegasperu (dot)" <jvenegasperu(at)gmail(dot)com>, Ayuda <pgsql-es-ayuda(at)postgresql(dot)org> |
Subject: | Re: Crear campo en todas las tablas |
Date: | 2017-10-22 13:34:25 |
Message-ID: | 5de5fc3c-c616-2bef-d777-33861da72d17@gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-es-ayuda |
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
From | Date | Subject | |
---|---|---|---|
Next Message | jvenegasperu . | 2017-10-22 17:18:26 | Re: Crear campo en todas las tablas |
Previous Message | jvenegasperu . | 2017-10-22 12:53:17 | Crear campo en todas las tablas |