Foros del Web » Programación para mayores de 30 ;) » Bases de Datos General » Mysql »

problema con if en procedimeinto almacenado

Estas en el tema de problema con if en procedimeinto almacenado en el foro de Mysql en Foros del Web. Hola a todos , bueno mi consulta es para saber que estoy haciendo mal. cree un procedimiento llamado insertarUsuario donde no debería permitir insertar un ...
  #1 (permalink)  
Antiguo 04/11/2015, 15:05
 
Fecha de Ingreso: noviembre-2015
Ubicación: chile
Mensajes: 5
Antigüedad: 8 años, 5 meses
Puntos: 0
Pregunta problema con if en procedimeinto almacenado

Hola a todos , bueno mi consulta es para saber que estoy haciendo mal.
cree un procedimiento llamado insertarUsuario donde no debería permitir insertar un usuario si tiene el código de socio repetido al crearlo.
MIS TABLAS SON:
| usuario | CREATE TABLE `usuario` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`NICK` varchar(20) NOT NULL,
`PASSWORD` varchar(8) NOT NULL,
`IDSOC` int(11) NOT NULL,
`ADMIN` char(2) NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`),
KEY `IDSOC` (`IDSOC`),
CONSTRAINT `usuario_ibfk_1` FOREIGN KEY (`IDSOC`) REFERENCES `socio` (`IDSOC`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=latin1 |


| socio | CREATE TABLE `socio` (
`IDSOC` int(11) NOT NULL AUTO_INCREMENT,
`SOC_IDSOC` int(11) DEFAULT NULL,
`SOC_IDSOC2` int(11) DEFAULT NULL,
`DIRECCION` text,
`TELEFONO` int(11) DEFAULT NULL,
`NOMBRE` varchar(20) NOT NULL,
PRIMARY KEY (`IDSOC`),
KEY `FK_AVALA` (`SOC_IDSOC2`),
KEY `FK_AVALA2` (`SOC_IDSOC`),
CONSTRAINT `FK_AVALA` FOREIGN KEY (`SOC_IDSOC2`) REFERENCES `socio` (`IDSOC`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_AVALA2` FOREIGN KEY (`SOC_IDSOC`) REFERENCES `socio` (`IDSOC`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1 |

MI PROCEDIMEINTO:
mysql> create procedure insertarUsuario(nick1 varchar(20),password1 varchar(8),idSoc1 int(11))
-> begin
-> declare total int;
-> set total = (select count(IDSOC) from usuario where NICK = nick1);
-> if total<2 then
-> insert into usuario (NICK,PASSWORD,IDSOC) values(nick1,password1,idSoc1);
-> end if;
-> commit;
-> end;
-> //
Query OK, 0 rows affected (0.00 sec)

no logro saber porque inserta el registro a pesar de haber un usuario con mas de un idSoc repetido.
Ayuda...
  #2 (permalink)  
Antiguo 04/11/2015, 15:13
Avatar de Libras
Colaborador
 
Fecha de Ingreso: agosto-2006
Ubicación: En la hermosa perla de occidente
Mensajes: 7.412
Antigüedad: 17 años, 8 meses
Puntos: 774
Respuesta: problema con if en procedimeinto almacenado

Prueba con:

Código MySQL:
Ver original
  1. Select @total:=count(idsoc) from tabla
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me
  #3 (permalink)  
Antiguo 04/11/2015, 15:30
 
Fecha de Ingreso: noviembre-2015
Ubicación: chile
Mensajes: 5
Antigüedad: 8 años, 5 meses
Puntos: 0
Respuesta: problema con if en procedimeinto almacenado

Probé tu código pero ahora no inserta ninguna fila (con o sin idsoc repetido)

mysql> create procedure insertarUsuario(nick1 varchar(20),password1 varchar(8),idSoc1 int(11))
-> begin
-> declare total int;
-> select @total:= count(IDSOC) from usuario where NICK = nick1;
-> if total<2 then
-> insert into usuario (NICK,PASSWORD,IDSOC) values(nick1,password1,idSoc1);
-> end if;
-> commit;
-> end;
-> //
Query OK, 0 rows affected (0.00 sec)

mysql> call insertarUsuario('naty','1234',6);
-> //
+-----------------------+
| @total:= count(IDSOC) |
+-----------------------+
| 3 |
+-----------------------+
Gracias por intentar :)
  #4 (permalink)  
Antiguo 04/11/2015, 15:34
Avatar de Libras
Colaborador
 
Fecha de Ingreso: agosto-2006
Ubicación: En la hermosa perla de occidente
Mensajes: 7.412
Antigüedad: 17 años, 8 meses
Puntos: 774
Respuesta: problema con if en procedimeinto almacenado

Me encanto el gracias por intentar......y porq no intentas agregando el @ a todas las llamadas de la variable....ahora si revisas lo q posteas el valor de total es 3, por lo q no va a insertar con la validacion de @total《 2
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me
  #5 (permalink)  
Antiguo 04/11/2015, 15:44
 
Fecha de Ingreso: noviembre-2015
Ubicación: chile
Mensajes: 5
Antigüedad: 8 años, 5 meses
Puntos: 0
Respuesta: problema con if en procedimeinto almacenado

tienes toda la razón, el error fue en la lógica de la prueba que realice ya estoy mariada con el código ...

gracias

Etiquetas: procedimiento_almacenado
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 19:36.