Ver Mensaje Individual
  #1 (permalink)  
Antiguo 09/11/2010, 06:40
cachimira
 
Fecha de Ingreso: noviembre-2010
Mensajes: 24
Antigüedad: 13 años, 6 meses
Puntos: 0
Otro error con Triggers

Buenos días,

Tengo un problema con el uso de Triggers.

Hace un tiempo hize un trigger que me evitó duplicaciones antes de realizar un insert en una tabla. Ahora debo pasar ese proyecto de mi ordenador a un servidor y resulta que los triggers que tenía funcionando han dejado de hacerlo.

Mi versión es la 5.1.41 y la del servidor es la 5.0.32. He estado buscando información sobre el cambio de sintaxis que debo aplicar, pero no hay manera de que me funcione.

Mi tabla es la siguiente:

Código PHP:
CREATE TABLE IF NOT EXISTS `user` (
  `
id_userint(11NOT NULL AUTO_INCREMENT,
  `
loginvarchar(255COLLATE utf8_bin DEFAULT NULL,
  `
passwordvarchar(255COLLATE utf8_bin DEFAULT NULL,
  `
statetinyint(1) DEFAULT NULL,
  `
namevarchar(255COLLATE utf8_bin DEFAULT NULL,
  `
descriptionvarchar(255COLLATE utf8_bin DEFAULT NULL,
  `
emailvarchar(255COLLATE utf8_bin DEFAULT NULL,
  `
data_inserttimestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  
PRIMARY KEY (`id_user`)
ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_bin
Y mi trigger inicial que funciona en versión 5.1.X es:

Código PHP:
DELIMITER //
CREATE TRIGGER user_no_repeat BEFORE INSERT ON user
 
FOR EACH ROW begin
  
if (select count(*) from user where login=new.login and password=new.password) > 0 then
    call fail
('Repeated insert into "user"');
  
end if;
 
end
//
DELIMITER 
Y el procedimiento al que llamo es:

Código PHP:
CREATE PROCEDURE Fail(_Message VARCHAR(255))
INSERT INTO error (MessageVALUES (_Message); 
Si intento ejecutar el Trigger en la versión 5.0.32 me da el siguiente fallo:

Error
Parece haber un error en su consulta de SQL. La salida generada por el servidor de MySQL, de existir, aparece abajo, en cuyo caso puede ayudar a diagnosticar el problema.
ERROR: Signo de puntuación desconocido @ 11
STR: //
SQL: DELIMITER //
CREATE TRIGGER user_no_repeat BEFORE INSERT ON user
FOR EACH ROW begin if (select count(*) from user where login=new.login and password=new.password) > 0 then call fail('Repeated insert into "user"');

consulta SQL:
DELIMITER // CREATE TRIGGER user_no_repeat BEFORE INSERT ON user FOR EACH ROW begin if (select count(*) from user where login=new.login and password=new.password) > 0 then call fail('Repeated insert into "user"');
MySQL ha dicho:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER //
CREATE TRIGGER user_no_repeat BEFORE INSERT ON user
FOR EACH ROW' at line 1


He probado a cambiar las // por &&, por %% y por ;; y nada, me da el mismo error.
Finalmente en algún sitio he visto que decía que funcionaba si quitabas los delimitadores, de hecho el procedimiento ‘Fail’ lo he creado quitando los delimiter. Así que sin ellos el error que obtengo es el siguiente:

Código PHP:
CREATE TRIGGER user_no_repeat BEFORE INSERT ON user
 
FOR EACH ROW begin
  
if (select count(*) from user where login=new.login and password=new.password) > 0 then 
   call fail
('Repeated insert into "user"');
  
end if;
 
end 
MySQL ha dicho:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4


Si ejecuto solamente la linea “call fail('Repeated insert into "user"');” me funciona perfectamente, incluso he intentado cambiar la llamada por el insert a la tabla de error directamente:

Código PHP:
CREATE TRIGGER user_no_repeat BEFORE INSERT ON user
 
FOR EACH ROW begin
  
if (select count(*) from user where login=new.login and password=new.password) > 0 then 
    insert into error 
(id_errormessagedata_insertvalues (null"Repeated insert into user"now());
  
end if;
 
end 
Pero tampoco:

MySQL ha dicho:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'insert into error (EID, message, data_insert) values (null, "Repeated insert int' at line 4

Y si ejecuto solo el insert, se realiza correctamente la inserción a la tabla de error.


La verdad es que he estado mirando y buscando alguna solución y no lo he encontrado… alguna idea?

Gracias!!!