Delimiter //
/**
function cash_autoincrement_value
Check if value could be cast to integer
**/
drop procedure if exists CASH_AUTOINCREMENT_VALUE//

create procedure CASH_AUTOINCREMENT_VALUE(
    in tablename varchar(255)
    , out  autoval Int Unsigned
)
Begin
    Set @value = 0;
    set @query = concat('Set @value = (Select max(ID_', tablename, ') From ', tablename, ');');
    
    PREPARE stmt FROM @query;
    execute stmt			
        ;
    Deallocate prepare stmt;
    
    set autoval = Coalesce(@value, 0);

--     Declare tc cursor For
-- 
--     select AUTO_INCREMENT
--     From information_schema.tables
--     where
--         table_name = tablename collate utf8_general_ci
--         and TABLE_SCHEMA = schema()
--     ;
--     Open tc;
--     Fetch tc into autoval;
-- 
--     close tc;

End //

Delimiter ;
