简体   繁体   English

如何使用游标创建存储过程以通过验证将数据从一个表移动到另一表(Mysql)

[英]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.

相关问题 如何使用mysql中的存储过程将记录从一个表保存到另一表? - How to save records from one table to another table using stored procedure in mysql? 将数据从一个 MySQL 表移动到另一个表 - Move data from one MySQL table to another 如何在MySQL中将数据从一个表移到另一个表? - How do I move data from one table into another in MySQL? 将数据从一个表转移到另一个表后,用于更改数据的存储过程? - Stored procedure for altering data after transferring it from one table to another? 如何通过传递参数来创建用于将数据从一个表插入到另一个表的存储过程 - How to create stored procedure for insert data from one table to other table by passing parameter 如何将一行从一个表移动到mysql中的另一个表? - How to move one row from one table, to another table in mysql? 从MySQL中的存储过程创建新表 - Create a new table from a stored procedure in MySQL 使用游标和临时表时,MySQL存储过程的性能问题 - MySQL stored procedure performance issue when using cursor and temporary table MySQL:如何在存储过程中动态创建表? - MySQL : How to Create table dynamically in stored procedure? 使用存储过程将数据从 csv 导入 Mysql 表 - Import data from a csv to Mysql table using a stored procedure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM