Nuevo campo en todas las tablas de la base de datos

From: mauricio pullabuestan <jmauriciopb(at)yahoo(dot)es>
To: Lista Postgres ES <pgsql-es-ayuda(at)postgresql(dot)org>
Subject: Nuevo campo en todas las tablas de la base de datos
Date: 2016-04-12 22:33:27
Message-ID: 1679270039.3080779.1460500407847.JavaMail.yahoo@mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda

Buen día.

Necesito crear un campo en todas que no tengan un campo en concreto para ello tengo 2 funciones, el problema se da es que la funcion
campo_check_fnc me devuelve también las vista y se produce un error.

Como puedo fitrar que solo me devuelva tablas.

CREATE OR REPLACE FUNCTION public.campo_check_fnc()
RETURNS void AS
$BODY$
DECLARE rs RECORD;
BEGIN
FOR rs IN
(
SELECT table_schema, table_name
FROM information_schema.columns
Where table_schema Not In ('pg_catalog', 'information_schema', 'public', 'prueba')
And table_schema || table_name Not IN
(Select t.table_schema || t.table_name
From
(
SELECT table_schema, table_name, column_name = 'mi_campo' As existe_campo
FROM information_schema.columns
Where table_schema Not In ('pg_catalog', 'information_schema', 'public', 'prueba')
) t
Where t.existe_campo = TRUE
)
GROUP BY table_schema, table_name
ORDER BY table_schema, table_name ) LOOP

PERFORM * FROM public.campo_crea_fnc(rs.table_schema::VARCHAR, rs.table_name::VARCHAR);
END LOOP;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION public.migracion_campo_check_fnc()
OWNER TO postgres;

CREATE OR REPLACE FUNCTION public.campo_crea_fnc(
p_schema character varying,
p_tabla character varying)
RETURNS void AS
$BODY$
BEGIN

EXECUTE 'ALTER TABLE ' || p_schema || '.' || p_tabla || ' ADD COLUMN mi_campo CHAR(2) DEFAULT ''NO''';
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;

Saludos.
Mauricio

-
Enviado a la lista de correo pgsql-es-ayuda (pgsql-es-ayuda(at)postgresql(dot)org)
Para cambiar tu suscripcin:
http://www.postgresql.org/mailpref/pgsql-es-ayuda

Responses

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message raul andrez gutierrez alejo 2016-04-12 23:32:31 triger NEW.record.campo
Previous Message Guillermo E. Villanueva 2016-04-12 04:12:14 StreaminReplication 1 + 2