There are many ways to do this, I will add one where I will use two variables defined as in your procedure in SQL
, num
will have the value of the rows that are currently in your table and id
the value generado
To generate the new code we used the function LEFT () , which will return the number of characters to the izquierda
specified in the second parameter.
SELECT LEFT('holamundo', 4); // retona 'hola'
With this in mind, the second parameter will be the amount total
of characters accepted by the column in your table (5)
, menos
the number of characters obtained with the function CHAR_LENGTH (I'm not sure if it's the best option for get the amount of an int) of the value returned by the function count
, all this will be concatenated with the value of num
, finally the Insert
of the values is realized.
CREATE PROCEDURE insertsertar(IN nombemple varchar(80) )
BEGIN
DECLARE num int;
DECLARE id char(5);
SET num = (SELECT COUNT(*)+1 FROM empleado);
SET id = CONCAT(LEFT('E0000', 5-CHAR_LENGTH(num)),num);
INSERT INTO empleado values (id ,nombemple);
END IF;