mysql - check if a select query is greater than 0

1

Hi, I'm creating a stored procedure for user registration. My stored procedure:

delimiter //
drop procedure if exists sp_insertar_usuario;
create procedure sp_insertar_usuario
(
in idpersona int,
in usuario varchar(20),
in pass varchar(45),
in imagen varchar(400),
in email varchar(45)
)
begin
insert into seg_usuario(perso_id, usu_nom, usu_pass, usu_imagen, usu_freg, usu_est, usu_email) 
values(idpersona, usuario, pass, imagen,now(),"Activo",email);
end //
delimiter ;

Everything works normal, the idea is that before doing the insert the procedure verifies if the idperson that is going to be used has already been used with another user. I was thinking something like this:

begin
set @resp = select count(*) from segu_usuario where perso_id = idpersona;
if @resp > 0 then
select 'REPETIDO'
else
insert into seg_usuario(perso_id, usu_nom, usu_pass, usu_imagen, usu_freg, usu_est, usu_email) 
values(idpersona, usuario, pass, imagen,now(),"Activo",email);
select 'OK'
end if;

end //

But the procedure is not created. Any solution, or alternative? I thank you in advance.

    
asked by Raphael 14.06.2017 в 19:22
source

1 answer

2

what you want is to check that the idPersona, for that there are fields in the database with constraint unique , I leave an example:

go
    ALTER TABLE seg_usuario
ADD CONSTRAINT NombreConstraint UNIQUE (idPersona);
go

With this, the database understands with unique that the value of that field can not be duplicated , so that, if you try to add another record with an equal value, an exception will skip . For more information, see this link

    
answered by 14.06.2017 / 19:28
source