如何在MySQL存储过程中使用多个游标和多个循环来获取数据?

时间:2018-09-29 06:11:15

标签: mysql sql stored-procedures

根据用例,此过程应返回30行,其中15个唯一的潜在顾客ID对应于activity = 1中的所有15个;随机10人参加活动= 2;从这10个线索中,有5个线索随机对应于activity = 3。

当我运行存储过程时,它会一直持续到填充25条记录为止。对应于活动1唯一获得15个ID,活动2唯一获得10个ID;但是,我无法立即获取记录。

如果您在代码中签入;我已经在关闭cur2之前提到了SELECT语句(SELECT COUNT(* FROM Task2),它应该返回Count = 25;但事实并非如此。因此,显然,该程序甚至没有深入到我为activity = 3声明的cur3中。

我也尝试过使用带有光标的Continue Handler;但这似乎没有用!

任何人都可以帮助解决此问题吗?

开始         如果存在则删除表task2;         创建表task2(                     ID int(11)NOT NULL AUTO_INCREMENT,                     type_id int(11)默认为空,                     url varchar(100)默认为空,                     lead_id int(11)默认为空,                     createdDate日期时间默认为空,                     month varchar(20)默认为空,                     year int(11)默认为空,                     month_year varchar(20)默认为空,                      主键(ID)                            )

BEGIN
    -- INSERTING DATA FOR ACTIVITY = 1

            DECLARE nurl VARCHAR(100);
            DECLARE nleadid INTEGER;
            DECLARE ncreateddate DATETIME;

            DECLARE l_count INTEGER;
            DECLARE loop_count INTEGER;

   -- LOOP COUNT = 15
            SET l_count = 15;

            SET loop_count = 1; 

            read_loop:LOOP

            IF loop_count > l_count THEN
            LEAVE read_loop;
            END IF; 


            IF loop_count = 1
            THEN
            SET nleadid = 100;
            SET ncreateddate = ('2012-09-08 01:09:30');             

            ELSE
            SET nleadid = 100 + loop_count - 1;
            SET ncreateddate = (SELECT MAX(createdDate) FROM task2);
            SET ncreateddate = DATE_ADD(ncreateddate, INTERVAL ELT(0.5 + RAND() * 6, '3', '5', '45', '34', '23', '68') MINUTE);                                      
            END IF;


            SET nurl = ELT(0.5 + RAND() * 3, 'g.com', 'y.com', 'm.com');

            INSERT INTO task2 (type_id, url, lead_id, createdDate)
            VALUES ('1', nurl, nleadid, ncreateddate);

            UPDATE task2
            SET month = MONTHNAME(createddate), year = YEAR(createddate), month_year = CONCAT(MONTHNAME(createddate),'-', YEAR(createddate));

            SET loop_count = loop_count + 1;

            END LOOP read_loop;

    END;

    -- INSERTING THE DATA FOR ACTIVITY = 2 
    BEGIN

                DECLARE nurl VARCHAR(100);
                DECLARE nleadid INTEGER;
                DECLARE ncreateddate DATETIME;

                DECLARE l_count INTEGER;
                DECLARE loop_count INTEGER;

        -- CURSOR DECLARATION TO FETCH 10 RANDOM RECORDS FROM ACTIVITY=1

                DECLARE cur2 CURSOR FOR
                SELECT DISTINCT lead_id FROM task2 WHERE type_id = 1 ORDER BY RAND() LIMIT 10;


                SET l_count = 10;

                SET loop_count = 1; 

                OPEN cur2;



                read_loop:LOOP

                FETCH cur2 INTO nleadid;

                IF loop_count > l_count THEN
                SELECT loop_count_2, l_count_2;
                LEAVE read_loop;
                END IF; 


                SET nurl = ELT(0.5 + RAND() * 3, 'g.com', 'y.com', 'm.com');

                SET ncreateddate = (SELECT MAX(createdDate) FROM task2 WHERE lead_id = nleadid);
                SET ncreateddate = DATE_ADD(ncreateddate, INTERVAL ELT(0.5 + RAND() * 6, '3', '5', '45', '34', '23', '68') MINUTE);

                INSERT INTO task2 (type_id, url, lead_id, createdDate)
                VALUES ('2', nurl, nleadid, ncreateddate);

                UPDATE task2
                SET month = MONTHNAME(createddate), year = YEAR(createddate), month_year = CONCAT(MONTHNAME(createddate),'-', YEAR(createddate));

                SET loop_count = loop_count + 1;
                SELECT COUNT(*) FROM task2;
                END LOOP read_loop;

        SELECT COUNT(*) FROM task2;
    CLOSE cur2;

END;

-- INSERTING DATA FOR ACTIVITY = 3
    BEGIN

            DECLARE nurl VARCHAR(100);
            DECLARE nleadid INTEGER;
            DECLARE ncreateddate DATETIME;
            DECLARE l_count INTEGER;
            DECLARE loop_count INTEGER;


   -- CURSOR DECLARATION FOR SELECTING 5 RANDOM LEADS FROM ACTIVITY=2     

            DECLARE cur3 CURSOR FOR
            SELECT DISTINCT lead_id FROM task2 WHERE type_id = 2 ORDER BY RAND() LIMIT 5;

            SET l_count = 5;

            SET loop_count = 1; 


            OPEN cur3;
            read_loop:LOOP

            IF loop_count > l_count THEN
            LEAVE read_loop;
            END IF; 

            FETCH cur3 INTO nleadid;

            SET nurl = CONCAT(ELT(0.5 + RAND() * 3, 'g.com', 'y.com', 'm.com'), ELT(0.5 + RAND() * 3, '/home.html', '/index.html', '/about.html'));
            SELECT nurl;              
            SET ncreateddate = (SELECT MAX(createdDate) FROM task2 WHERE lead_id = nleadid);

            SET ncreateddate = DATE_ADD(ncreateddate, INTERVAL ELT(0.5 + RAND() * 6, '3', '5', '45', '34', '23', '68') MINUTE);

SELECT ncreateddate;     

            INSERT INTO task2 (type_id, url, lead_id, createdDate)
            VALUES ('3', nurl, nleadid, ncreateddate);

            UPDATE task2 
            SET month = MONTHNAME(createddate), year = YEAR(createddate), month_year = CONCAT(MONTHNAME(createddate),'-',YEAR(createddate));

            SET loop_count =loop_count + 1;
            END LOOP read_loop;
    CLOSE cur3;
END;

    SELECT * FROM task2;

END

0 个答案:

没有答案