[英]How to create stored procedure using cursor to move data from one table to another table with validation ( Mysql)
I have to move data from resource table to event table to remove resource table. 我必须将数据从资源表移至事件表以删除资源表。 Consider, Table schemas as follows, mysql> desc event
考虑,表架构如下,mysql> desc事件
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| event_id | bigint(20) | NO | PRI | NULL | auto_increment |
| path | varchar(255) | YES | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
mysql> desc resource; mysql> desc资源;
+--------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+--------------+------+-----+---------+----------------+
| resource_id | bigint(20) | NO | PRI | NULL | auto_increment |
| jcr_id | varchar(500) | YES | | NULL | |
+--------------------+--------------+------+-----+---------+----------------+
event path contain jcr_id or resource_id, if it contain jcr_id then entry from resouce table else update event path value with jcr_id and then delete. 事件路径包含jcr_id或resource_id,如果它包含jcr_id,则从资源表中输入,否则使用jcr_id更新事件路径值,然后删除。
Consider following Psuedo code while iterating every resource_id, 在迭代每个resource_id时,请考虑遵循Psuedo代码,
result = select event_id from event where path=_resource_id;
if result is empty
delete from resource where resource_id= _resource_id;
else
update event set path=jcr_id where event_id= result;
delete from resource where resource_id= _resource_id;
Try this. 尝试这个。
DELIMITER //
CREATE PROCEDURE `new_procedure` ()
BEGIN
declare row_min int;
declare row_max int;
declare resource_id int;
select 1, max(resource_id)
into row_min, row_max
from resource;
while row_min <= row_max
do
select resource_id into resource_id
from resource
where resource_id = row_min;
-- Iterating for every record. Add your update code here.
set row_min = row_min + 1;
end while;
END//
DELIMITER ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.