MySQL存储过程游标示例

时间:2019-01-31 11:17:03

标签: mysql

下面的代码运行良好,我需要问如何使用变量而不是游标select语句中编写的表名?

block5: begin
    declare var1 int default 0; 

    DECLARE done TINYINT DEFAULT FALSE;
    DECLARE cursor1 CURSOR FOR SELECT co_srno FROM vu_company where katscode in ('OGDC', 'LUCK', 'MCB');
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

    OPEN cursor1;

    my_loop: LOOP 
        FETCH cursor1 INTO var1;
        IF done THEN 
            LEAVE my_loop;
        ELSE
            set @s = concat("insert into ", @temp_table_name_scrnr_data, " (sr_no) values (", var1, ")"); 
            PREPARE stmt FROM @s; 
            EXECUTE stmt; 
            DEALLOCATE PREPARE stmt; 
        END IF;
    END LOOP;
end block5;

0 个答案:

没有答案