MySQL存储过程不插入数据

时间:2019-05-10 04:55:26

标签: mysql procedure database-cursor

实际上,我正在尝试使用游标将一些数据插入表中,并且没有任何错误。它只是说:

Query executed OK, 0 rows affected.

我正在尝试从testdatabase.wp_posts p表的每个字段中获取数据,并将其插入到现有的表testdatabase.wp_postmeta中。

我的语法是:

DROP PROCEDURE IF EXISTS add_authors_to_prod;
    DELIMITER $$
CREATE PROCEDURE add_authors_to_prod()
    BEGIN
        DECLARE  v_post_id        INT;
        DECLARE  v_author_id      INT;
        DECLARE  v_last_author    INT DEFAULT 0;
        DECLARE  str              VARCHAR(500);
        DECLARE  v_first_id       INT DEFAULT 10916;

        DECLARE author_csr CURSOR FOR
        SELECT n.nid, p.id FROM testdatabase.wp_posts p
            JOIN drupaltest.srmaif_field_data_field_auther_list auth ON auth.entity_id = p.id
            JOIN drupaltest.node_field_data n ON auth.field_auther_list_nid = n.nid
            ORDER BY p.id;

        DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_last_author = 1;

        START TRANSACTION;
            OPEN author_csr;
                author_loop:LOOP
                    FETCH author_csr INTO v_author_id, v_post_id;
                    IF  v_last_author THEN
                        SET str = CONCAT(str, '}');
                        LEAVE author_loop;
                    END IF;

                    IF v_first_id <> v_post_id THEN
                        SET v_first_id = v_post_id;
                        SET str = CONCAT(str, '}');
                        INSERT INTO testdatabase.wp_postmeta (post_id, meta_key, meta_value)
                             VALUES(v_post_id, 'authors', CONCAT(str, v_author_id));
                        SET str = 'a:2:{';
                    ELSE
                        SET str = CONCAT(str,'i:', v_author_id, ';s:5:\"', v_author_id, '\";');
                    END IF;
                END LOOP author_loop;
            CLOSE author_csr;
            SET v_last_author=0;
END$$
DELIMITER ;

CALL add_authors_to_prod()

0 个答案:

没有答案