I don’t know a native way to do that, something similar to Rest Parameter javascript, usually in this type of situation, a type parameter is created varchar
that will receive all data already separated by comma:
delimiter //
create procedure excluir_usuario (in codigo_empresas varchar(255))
begin
set @sql = concat('delete from socio_pessoa_juridica where emp_codigo in (', codigo_empresas, ')');
prepare statement from @sql;
execute statement;
deallocate prepare statement;
set @sql = concat('delete from empresa where emp_codigo in (', codigo_empresas, ')');
prepare statement from @sql;
execute statement;
deallocate prepare statement;
end
//
delimiter ;
With this, we can pass different quantities of parameters to the in
in the where
, making it a little more dynamic, but still limited:
set @codigo_das_empresas = '1,2,3,4,5' ;
call excluir_usuario(@codigo_das_empresas) ;
See online: https://www.db-fiddle.com/f/cpmMCYjwNYeaTDgQ3pRE6g/2
Another way, which I believe is more about receiving several different parameters, is to receive a json as a parameter:
delimiter //
create procedure excluir_usuario (in params json)
begin
set @empresas = json_unquote(json_extract(params, '$.empresas'));
set @sql = concat('delete from socio_pessoa_juridica where emp_codigo in (', @empresas, ')');
prepare statement from @sql;
execute statement;
deallocate prepare statement;
set @sql = concat('delete from empresa where emp_codigo in (', @empresas, ')');
prepare statement from @sql;
execute statement;
deallocate prepare statement;
end
//
delimiter ;
Here I could receive different parameters, in different ways, I believe that the example of varchar already meets you, but here is the example, maybe give you other ideas:
set @codigo_das_empresas = json_object( 'empresas', '1,2,3,4,5' );
call excluir_usuario(@codigo_das_empresas) ;
See online: https://www.db-fiddle.com/f/9gqgYjvMSbSDpTymp4vF48/0