【问题标题】:How to create stored procedure using cursor to move data from one table to another table with validation ( Mysql)如何使用游标创建存储过程以通过验证将数据从一个表移动到另一个表(Mysql)
【发布时间】:2017-10-16 06:59:57
【问题描述】:

我必须将数据从资源表移动到事件表才能删除资源表。 考虑,表模式如下, mysql> desc 事件

+-----------------+--------------+------+-----+---------+----------------+
| Field           | Type         | Null | Key | Default | Extra          |
+-----------------+--------------+------+-----+---------+----------------+
| event_id        | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| path            | varchar(255) | YES  |     | NULL    |                |
+-----------------+--------------+------+-----+---------+----------------+

mysql> desc 资源;

+--------------------+--------------+------+-----+---------+----------------+
| Field              | Type         | Null | Key | Default | Extra          |
+--------------------+--------------+------+-----+---------+----------------+
| resource_id        | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| jcr_id             | varchar(500) | YES  |     | NULL    |                |
+--------------------+--------------+------+-----+---------+----------------+

事件路径包含jcr_id或resource_id,如果它包含jcr_id,则从资源表中输入,否则用jcr_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;

【问题讨论】:

  • 移动数据的第一步是学习如何格式化问题中的数据。
  • 是的,正在处理中
  • 请用合适的例子说明需求。
  • @HatimStovewala 您可以考虑以伪代码为例,我想使用查询“从 path=resource_id 的事件中选择 event_id”id 来迭代每个 resource_id 的资源表,我找到了此查询的任何结果然后我有使用 jcr_id 更新路径值并从资源表中删除条目。
  • 我的回答有帮助吗?

标签: mysql stored-procedures cursor


【解决方案1】:

试试这个。

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 ;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-09
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    • 2010-12-22
    • 2013-10-18
    • 1970-01-01
    相关资源
    最近更新 更多