#mysql mysql存储过程 表名作为变量的问题

大概意思是想要从TS_SCFZ表中的tabname字段来历遍所有表,从而查找是否有符合条件的记录
请大神帮帮忙
现在报错了

drop procedure if exists deletecheck;
delimiter //
CREATE DEFINER = `root`@`%` PROCEDURE `deletecheck`(IN vahdm varchar(20))
BEGIN 
    DECLARE done BOOLEAN DEFAULT FALSE;
    DECLARE vtable VARCHAR(24);
    DECLARE cur CURSOR FOR select tabname from TS_SCFZ where SFSC = 1;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
    OPEN cur;
    read_loop: LOOP
      FETCH cur INTO vtable;
      IF done THEN
        LEAVE read_loop;
      END IF;
      START TRANSACTION;
          set @sqlcmd = concat("select (1) from ",vtable," where ahdm = vahdm");
          prepare stmt from @sqlcmd;
          IF EXISTS(execute stmt)
            THEN
            select concat('table is ',vtable);
          ELSE
            select concat(vtable,' is clean');
            deallocate prepare stmt;
            END IF;
        COMMIT;
    END LOOP read_loop;
    CLOSE cur;
END

drop procedure if exists deletecheck;
delimiter //
CREATE DEFINER = root@% PROCEDURE deletecheck(IN vahdm varchar(20))
BEGIN
DECLARE done BOOLEAN DEFAULT FALSE;
DECLARE vtable VARCHAR(24);
DECLARE cur CURSOR FOR select tabname from TS_SCFZ where SFSC = 1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
read_loop: LOOP
FETCH cur INTO vtable;
IF done THEN
LEAVE read_loop;
END IF;
START TRANSACTION;
set @sqlcmd = concat("select (1) from ",vtable," where ahdm = vahdm");
prepare stmt from @sqlcmd;
IF EXISTS(execute stmt)
THEN
select concat('table is ',vtable);
ELSE
select concat(vtable,' is clean');
deallocate prepare stmt;
END IF;
COMMIT;
END LOOP read_loop;
CLOSE cur;
END

[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'execute stmt)
            THEN
            select concat('table is ',vtable);
          ELSE
            sele' at line 16
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问