zoukankan      html  css  js  c++  java
  • mysql 存储过程实现从一张表数据迁移到另一种表

        本文档主要是描述怎么在一系列条件下将home_brand_manual_pdf的数据插入到brand_shuomingshu表中

    1.首先数据的插入涉及到三张表,分别是home_brand(品牌),home_brand_manual_pdf(品牌pdf手册),brand_shuomingshu(品牌说明书)(此表刚开始没数据,是空表)

    2.表结构分别介绍

    home_brand( 品牌):

     home_brand_manual_pdf (品牌pdf手册):

    brand_shuomingshu(品牌说明书)

    3.表迁移需要的一系列条件:

                        home_brand_manual_pdf与brand_shuomingshu表列以及home_brand的关系如下

               (1)列对应满足以下关系

              

    home_brand_manual_pdf

               brand_shuomingshu

       home_brand

         id

          pdfId

     

                 brandId

             id(很重要)

                  name

          pdfName

     

                  url

           url    

     

        downloadUrl

          downloadUrl

     

         site

          site  

        appId

          appId

     

        path

          pdfPath

     

        siteMemo

          memo

     

        size

          pdfSize

     

        error

          pdfError  

        totals

          pdfTotals  

        check

          pdfCheck  

        text

          pdfText  

        status

          pdfStatus  

        createUser

          createUser

     

        createDate

          createDate

     
        updateDate

          updateDate

     

        updateUser

          updateUser  

           千万注意:

             brandId列的值:  通过brand_shuomingshu.brandName到home_brand表中,根据name查询,得到brand信息,将对应的id,作为home_brand_manual_pdf.brandId

        (2)过滤条件brand_shuomingshu表:status > 0 and pdfStatus > 0(即只需要将该条件下的数据迁移到brand_shuomingshu中)

    4.通过存储过程迁移数据,通过navicat(mysql可视化根据)建立的存储过程如下(假设存储过程名字为test):

    BEGIN
    -- 需要定义接收游标数据的变量 
      DECLARE idp VARCHAR(255); 
      DECLARE brandIdp VARCHAR(255); 
      DECLARE namep VARCHAR(1000); 
      DECLARE urlp VARCHAR(1000);
    	DECLARE downloadUrlp VARCHAR(1000); 
    	DECLARE sitep VARCHAR(255);
    	DECLARE appIdp VARCHAR(255);
      DECLARE pathp VARCHAR(255);
      DECLARE siteMemop VARCHAR(255);
    	DECLARE sizep BIGINT(255); 
    	DECLARE errorp VARCHAR(255); 
    	DECLARE totalsp int(10);
    	DECLARE checkp int(11);
    	DECLARE textp  int(11);
    	DECLARE statusp TINYINT(255); 
    	DECLARE createUserp  VARCHAR(255);
    	DECLARE createDatep  TIMESTAMP;
    	DECLARE updateDatep  TIMESTAMP;
      DECLARE updateUserp  VARCHAR(255); 
      -- 遍历数据结束标志
      DECLARE done INT DEFAULT FALSE;
    
      DECLARE cur CURSOR FOR SELECT
    		brand_shuomingshu.pdfId AS id,
    		home_brand.id AS brandId,
    		brand_shuomingshu.pdfName AS `name`,
    		brand_shuomingshu.url AS url,
    		brand_shuomingshu.downloadUrl AS downloadUrl,
    		brand_shuomingshu.site AS site,
    		brand_shuomingshu.appId AS appId,
        brand_shuomingshu.pdfPath As path,
        brand_shuomingshu.memo AS siteMemo,
    		brand_shuomingshu.pdfSize AS size,
    		brand_shuomingshu.pdfError AS error,
    		brand_shuomingshu.pdfTotals AS totals,
    		brand_shuomingshu.pdfCheck AS `check`,
    		brand_shuomingshu.pdfText AS text,
    		brand_shuomingshu.pdfStatus AS `status`,
    		brand_shuomingshu.createUser AS createUser,
    		brand_shuomingshu.createDate AS createDate,
    		brand_shuomingshu.updateUser AS updateUser,
    		brand_shuomingshu.updateDate AS updateDate
    		from brand_shuomingshu,home_brand where  brand_shuomingshu.brandName = home_brand.`name` AND brand_shuomingshu.`status` >0 
     AND brand_shuomingshu.pdfStatus>0;
    
    
      -- 将结束标志绑定到游标
      DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
      -- 打开游标
      OPEN cur;
      
      -- 开始循环
      read_loop: LOOP
        -- 提取游标里的数据
        FETCH cur INTO idp,brandIdp,namep,urlp,downloadUrlp,sitep,appIdp,pathp,siteMemop,sizep,errorp,totalsp,checkp,textp,statusp,createUserp,createDatep,updateUserp,updateDatep;
        -- 声明结束的时候
        IF done THEN
          LEAVE read_loop;
        END IF;
        -- 这里做你想做的循环的事件
    
        INSERT INTO home_brand_manual_pdf VALUES (idp,brandIdp,namep,urlp,downloadUrlp,sitep,appIdp,pathp,siteMemop,sizep,errorp,totalsp,checkp,textp,statusp,createUserp,createDatep,updateUserp,updateDatep);
    
      END LOOP;
      -- 关闭游标
      CLOSE cur;
    
    END
    

      

     

      然后执行该存储过程call test即可

  • 相关阅读:
    Linux Centos7(Mac) 安装Docker教程
    SpringBoot应用操作Rabbitmq(fanout广播高级操作)
    SpringBoot应用操作Rabbitmq(topic交换器高级操作)
    SpringBoot应用操作Rabbitmq(direct高级操作)
    CCF 字符串匹配(find()函数的使用)
    PKU 1204 Word Puzzles(AC自动机)
    PKU 1932 XYZZY(Floyd+Bellman||Spfa+Floyd)
    PKU 1201 Intervals(差分约束系统+Spfa)
    PKU 2352 Stars(裸一维树状数组)
    PKU 3169 Layout(差分约束系统+Bellman Ford)
  • 原文地址:https://www.cnblogs.com/xiaolin-peter/p/8076010.html
Copyright © 2011-2022 走看看