截断所有记录的过程

时间:2010-07-22 08:19:16

标签: mysql

该程序将错误视为:

Script line: 4  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 'cmd;
  EXECUTE stmt;
  DROP PREPARE stmt;

- 循环结束    U'在第25行

请任何人纠正我,感谢你的建议......

DELIMITER $$

DROP PROCEDURE IF EXISTS `CR_SP_TRUNCATE1` $$
CREATE PROCEDURE `CR_SP_TRUNCATE1` ()
BEGIN

-- Declare local variables
DECLARE done BOOLEAN DEFAULT 0;
DECLARE cmd VARCHAR(2000);

-- Declare the cursor
DECLARE cmds CURSOR FOR
SELECT CONCAT('TRUNCATE TABLE ', TABLE_NAME) FROM INFORMATION_SCHEMA.TABLES
WHERE  TABLE_SCHEMA ='icbadwh' and TABLE_NAME LIKE 'cr%';

-- Declare continue handler
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1;

 -- Open the cursor
OPEN cmds;

-- Loop through all rows
REPEAT

  -- Get order number
  FETCH cmds INTO cmd;

  -- Execute the command
  PREPARE stmt FROM cmd;
  EXECUTE stmt;
  DROP PREPARE stmt;


-- End of loop
UNTIL done END REPEAT;

-- Close the cursor
CLOSE cmds;

END $$

DELIMITER ;

1 个答案:

答案 0 :(得分:0)

老问题,但我碰到了类似的东西。解决问题的解决方法(虽然我不是100%确定原因),如下:

FETCH cmds INTO cmd;
SET @s = cmd; -- copy query literal to user var

PREPARE stmt FROM @s; -- run prepare on user variable.
EXECUTE stmt;
DROP PREPARE stmt;

PREPARE期望的一些more info