Re: Crear campo en todas las tablas

From: "jvenegasperu (dot)" <jvenegasperu(at)gmail(dot)com>
To: Anthony Sotolongo <asotolongo(at)gmail(dot)com>
Cc: Ayuda <pgsql-es-ayuda(at)postgresql(dot)org>
Subject: Re: Crear campo en todas las tablas
Date: 2017-10-22 17:18:26
Message-ID: CA+KjtGdKBFKBK+D9MtXw35ozxyAOgA5nSe=6igf3_xzjW-rKEQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda

Gracias Anthony

funciono perfecto probado con postgres 9.5 todo bien. me ahorraste la tarde
jejeje.

El 22 de octubre de 2017, 8:34, Anthony Sotolongo <asotolongo(at)gmail(dot)com>
escribió:

> 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
>>
>
>

--
José Mercedes Venegas Acevedo
cel Mov RPC 964185205

Member of the PHP Documentation Group (Spanish)

In response to

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message Gustavo Vaccaro 2017-10-24 12:06:45 Replicacion con Postgres 10
Previous Message Anthony Sotolongo 2017-10-22 13:34:25 Re: Crear campo en todas las tablas