1
resposta

Error code: 1264

select f_cliente_aleatorio() LIMIT 0, 10000    Error Code: 1264. Out of range value for column 'f_cliente_aleatorio()' at row 1    0.000 sec

Minha função f_numero_aleatorio:

CREATE DEFINER=`root`@`localhost` FUNCTION `f_numero_aleatorio`(min int, max int) RETURNS int(11)
BEGIN
    declare vRetorno int;
    select floor((rand() * (max - min + 1)) + min) into vRetorno;
RETURN vRetorno;
END

Minha função f_cliente_aleatorio:

CREATE DEFINER=`root`@`localhost` FUNCTION `f_cliente_aleatorio`() RETURNS int(11)
BEGIN
    declare vRetorno varchar(11);
    declare num_max_tabela int;
    declare num_aleatorio int;
    select count(*) into num_max_tabela from tabela_de_clientes;
    set num_aleatorio = f_numero_aleatorio(1, num_max_tabela);
    set num_aleatorio = num_aleatorio - 1;
    select CPF into vRetorno from tabela_de_clientes limit num_aleatorio, 1;
RETURN vRetorno;
END
1 resposta

Olá, Renan, tudo bem?

Um erro é retornado ao executar a sua function f_cliente_aleatorio() , pois, o tipo do RETURNS que está como inteiro é diferente do valor que será retornado, que é um varchar.

Tente executar a sua function da seguinte forma:

CREATE DEFINER=`root`@`localhost` FUNCTION `f_cliente_aleatorio`() RETURNS varchar(11)
BEGIN
    declare vRetorno varchar(11);
    declare num_max_tabela int;
    declare num_aleatorio int;
    select count(*) into num_max_tabela from tabela_de_clientes;
    set num_aleatorio = f_numero_aleatorio(1, num_max_tabela);
    set num_aleatorio = num_aleatorio - 1;
    select CPF into vRetorno from tabela_de_clientes limit num_aleatorio, 1;
RETURN vRetorno;
END

Espero ter ajudado e bons estudos!

Caso este post tenha lhe ajudado, por favor, marcar como solucionado ✓. Bons Estudos!

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software