clear markdown feedback
clear markdown feedback
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 3364508 fiddles created (36258 in the last week).

SELECT VERSION();
VERSION()
10.3.15-MariaDB-1:10.3.15+maria~stretch-log
 hidden batch(es)


DROP TABLE IF EXISTS `test`;
 hidden batch(es)


CREATE TABLE IF NOT EXISTS `test` ( `contador` BIGINT UNSIGNED ) SELECT 1000 `contador`;
 hidden batch(es)


DROP PROCEDURE IF EXISTS `obtener_ultimo_registro`;
 hidden batch(es)


CREATE PROCEDURE `obtener_ultimo_registro` ( IN `tabl` VARCHAR(64) CHARSET utf8, IN `campo` INT ) NOT DETERMINISTIC NO SQL SQL SECURITY DEFINER SELECT * FROM `tabl` ORDER BY `campo` DESC LIMIT 1;
 hidden batch(es)


-- CALL `obtener_ultimo_registro`('test', 'contador'); -- ERROR
 hidden batch(es)


DROP PROCEDURE IF EXISTS `obtener_ultimo_registro`;
 hidden batch(es)


CREATE PROCEDURE `obtener_ultimo_registro` ( IN `tabl` VARCHAR(64) CHARSET utf8, IN `campo` INT ) NOT DETERMINISTIC NO SQL SQL SECURITY DEFINER EXECUTE IMMEDIATE CONCAT('SELECT * FROM `', `tabl`, '` ORDER BY ', `campo`, ' DESC LIMIT 1');
 hidden batch(es)


CALL `obtener_ultimo_registro`('test', 1);
contador
1000
 hidden batch(es)


DROP PROCEDURE IF EXISTS `obtener_ultimo_registro`;
 hidden batch(es)


CREATE PROCEDURE `obtener_ultimo_registro` ( IN `tabl` VARCHAR(64) CHARSET utf8, IN `campo` INT ) NOT DETERMINISTIC NO SQL SQL SECURITY DEFINER BEGIN SET @`sql` := CONCAT('SELECT * FROM `', `tabl`, '` ORDER BY ', `campo`, ' DESC LIMIT 1'); PREPARE `stmt` FROM @`sql`; EXECUTE `stmt`; DEALLOCATE PREPARE `stmt`; END;
 hidden batch(es)


CALL `obtener_ultimo_registro`('test', 1);
contador
1000
 hidden batch(es)