Re: Crear campo en todas las tablas

2017-10-22 Thread jvenegasperu .
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 
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)


Re: Crear campo en todas las tablas

2017-10-22 Thread Anthony Sotolongo
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





Crear campo en todas las tablas

2017-10-22 Thread jvenegasperu .
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