我遇到这种情况: 两个数据库中的两个表: 1st:`persone` .T_Persone`包含有关worker的详细信息以及默认为空的两个字段:`user_id`和`username`; 2nd:`joomla` .fe48y_users`,包含CMS Joomla中用户的信息,包括`id`和`username`。 我创建了一个在joomla中插入新用户的过程,并返回创建的新用户的user_id和用户名。 我还创建了2个触发器(在更新和插入时),它们调用该过程并在`T_Persone`表上设置user_id和username。 我还创建了一个删除`joomla` .fe48y_users`的触发器,它更新`persone` .T_Persone`并将null设置为`user_id`和`username`字段。
这是程序:
CREATE DEFINER=`root`@`localhost` PROCEDURE `persone_users_upd`(IN `in_id` INT(10) UNSIGNED, IN `new_email` VARCHAR(255), IN `old_email` VARCHAR(255), OUT `out_user_id` INT(10) UNSIGNED, OUT `out_username` VARCHAR(255))
BEGIN
DECLARE has_email Boolean;
DECLARE my_user_id INT;
DECLARE my_username VARCHAR(255);
DECLARE my_name VARCHAR(255);
SELECT (CASE new_email WHEN '' THEN FALSE ELSE TRUE END) INTO @has_email;
IF @has_email THEN
SELECT CONCAT(`Nome`,' ',`Cognome`), COALESCE(`CF`,LOWER(SUBSTR(new_email, 1, INSTR(new_email, '@') - 1))) INTO @my_name, @my_username FROM `T_Persone` WHERE `IDPersona` = in_id;
IF new_email = old_email THEN
INSERT INTO `spmsf`.`fe48y_users` (`name`, `username`, `email`, `password`, `block`, `sendEmail`, `registerDate`, `lastvisitDate`, `activation`, `params`, `lastResetTime`, `resetCount`, `otpKey`, `otep`, `requireReset`) VALUES
(@my_name, @my_username, new_email, '<omissis>', 0, 1, NOW(), '0000-00-00 00:00:00', '', '{"admin_style":"","admin_language":"","language":"","editor":"","helpsite":"","timezone":"Europe/Rome"}', '0000-00-00 00:00:00', 0, '', '', 0) ON DUPLICATE KEY UPDATE `name` = VALUES(`name`), `email` = VALUES(`email`);
ELSE
UPDATE `spmsf`.`fe48y_users` SET `email` = @my_email WHERE `email` = new_email;
END IF;
SELECT `id`, `username` INTO @my_user_id, @my_username FROM `spmsf`.`fe48y_users` WHERE `email` = new_email;
INSERT IGNORE INTO `spmsf`.`fe48y_user_usergroup_map` (`user_id`,`group_id`) VALUES (@my_user_id, 10);
SET out_user_id=@my_user_id, out_username=@my_username;
END IF;
END
这些是`T_Persone`的触发器:
CREATE TRIGGER `persone_del` AFTER DELETE ON `T_Persone`
FOR EACH ROW BEGIN
IF OLD.`user_id` IS NOT NULL THEN
DELETE FROM `spmsf`.`fe48y_users` WHERE `id` = OLD.`user_id`;
DELETE FROM `spmsf`.`fe48y_user_usergroup_map` WHERE `user_id` = OLD.`user_id`;
END IF;
END
CREATE TRIGGER `persone_ins` BEFORE INSERT ON `T_Persone`
FOR EACH ROW BEGIN
CALL persone_users_upd(NEW.`IDPersona`,COALESCE(NEW.`Email`,NEW.`Email_alt`),COALESCE(NEW.`Email`,NEW.`Email_alt`), @user_id, @username);
SET NEW.`user_id` = @user_id, NEW.`username` = @username;
END
CREATE TRIGGER `persone_upd` BEFORE UPDATE ON `T_Persone`
FOR EACH ROW BEGIN
CALL persone_users_upd(NEW.`IDPersona`,COALESCE(NEW.`Email`,NEW.`Email_alt`),COALESCE(OLD.`Email`,OLD.`Email_alt`), @user_id, @username);
SET NEW.`user_id` = @user_id, NEW.`username` = @username;
END
这是`fe48y_users` delete的触发器:
CREATE TRIGGER `users_del` BEFORE DELETE ON `fe48y_users`
FOR EACH ROW BEGIN
DECLARE my_user_id Int;
SELECT COUNT(*) INTO @my_user_id FROM `personale`.`T_Persone` WHERE `user_id` = OLD.`id`;
IF @my_user_id > 0 THEN
UPDATE `personale`.`T_Persone` SET `user_id` = NULL, `username` = NULL WHERE `user_id` = OLD.`id`;
END IF;
END
所以,我有两个问题:
1st:当我尝试删除`fe48y_users`中的用户时出现错误
#1442 - Can't update table 'fe48y_users' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
第二:当我插入一个新人时,它不会出现在'fe48y_users'中,但是当我更新它时,它不会出现。