數據遷移存儲過程(一張錶轉移到另一張表)

 本文檔主要是描述怎麼在一系列條件下將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):

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

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.`nameAND 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即可

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章