lunes, 3 de noviembre de 2014

como TRUNCATE las tablas de un schema en postgres psql

desde psql:

crear estas funciones:

create or replace function commacat( text, text ) returns text as $$ select $1 || ',' || $2; $$ language SQL;
create or replace function remove_comma( text ) returns text as $$ select trim( leading ',' from $1 ); $$ language SQL;
create aggregate stragg (
    sfunc = commacat,
    basetype = text,
    stype = text,
    initcond = '',
    finalfunc = remove_comma
);


luego hacer la selección de las tablas de tu schema y guardarla en una variable mediante el método gset. en este caso mi esquema se llama 'admin'


select stragg('admin.'|| TABLE_NAME) as totrunc from INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' and table_schema = 'admin' ; \gset

por ultimo, truncar las tablas que se guardaron en la varialble :totrunc


truncate :totrunc cascade;
TRUNCATE TABLE



No hay comentarios:

Publicar un comentario