Ver Mensaje Individual
  #1 (permalink)  
Antiguo 13/04/2016, 02:21
andelval
 
Fecha de Ingreso: abril-2016
Mensajes: 1
Antigüedad: 8 años
Puntos: 0
Función comprobar existencia tabla (nombre tabla por parámetro)

Buenas,

después de mucho pelear me decido a hacer mi primera pregunta. Estoy usando MySQL Server 5.7 y tengo el siguiente código:

Código:
delimiter //

create function table_exists(str_table char(255)) returns boolean
begin

	declare to_return boolean;
    
    select count(table_name)>0 as NUM_REG
    into to_return
    from information_schema.TABLES
    where table_name = concat("'",str_table,"'")
    limit 1;
    
    set to_return = false;
    if(to_return=true) then
		set to_return = true;
	end if;
    
    return to_return;
    
end
//

Haciendo pruebas en una query aparte, almaceno el nombre de mi tabla en una variable y trato de utilizarla (como si fuera el parámetro de entrada de mi función):

Código:
select @cuTable:=concat("'",'e3g_alava',"'");
select @cuTable;
Aquí @CuTable = 'e3g_alava'

Si hago y hago las siguientes 4 pruebas, no doy con la tecla para que me de los 2 resultados que debe haber en information_schema.TABLES:

Código:
select *
from information_schema.TABLES

where table_name = @cuTable;					-- opción 1
where table_name in (select @cuTable);				-- opción 2
where table_name concat("'",@cuTable,"'");			-- opción 3
where table_name = concat("'",'e3g_alava',"'");		-- intento desesperado...
¿Alguna idea? Muchas gracias!
Saludos