Parámetros entrada y salida:
CREATE PROCEDURE `test`(in id int, out p_actor varchar(100))
BEGIN
select concat_ws(' ',first_name,last_name) into p_actor from actor where actor_id=id;
END
call test(2, @actor);
CREATE DEFINER=`root`@`localhost` PROCEDURE `suma`(in a int, in b int, out suma int)
BEGIN
set suma=a+b;
END
call suma(8,9,@res);
Alta de registros con comprobación incluída:
CREATE DEFINER=`root`@`localhost` PROCEDURE `alta_actor`(in nombre varchar(100), in apellido varchar(100))
BEGIN
declare c int;
select count(*) into c from actor where first_name=nombre and last_name=apellido;
if c=0 and length(nombre)>1 and length(apellido)>1 then
insert into actor (first_name, last_name) values (nombre, apellido);
end if;
END
call alta_actor ('juan','pa');
call alta_actor ('juan','pa'); --No funcionará por repetido
call alta_actor ('juan','p'); -- no funcionará por longitud
Ejemplo de cursor:
CREATE PROCEDURE `cursor_ejemplo`(out total int) BEGIN DECLARE final int DEFAULT 0; declare nombre varchar(100); DECLARE mi_cursor CURSOR FOR SELECT first_name FROM actor; DECLARE CONTINUE HANDLER FOR NOT FOUND SET final = 1; set total=0; OPEN mi_cursor; while final=0 do fetch mi_cursor into nombre; if nombre like '%z%' then set total=total+1; end if; end while; CLOSE mi_cursor; END call cursor_ejemplo(@t);