开发者

truncate procedure for all records

the procedure is giving error as a:

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;

-- End of loop U' at line 25

pls correct me any one,thanking u in advqance...

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
  PR开发者_Go百科EPARE stmt FROM cmd;
  EXECUTE stmt;
  DROP PREPARE stmt;


-- End of loop
UNTIL done END REPEAT;

-- Close the cursor
CLOSE cmds;

END $$

DELIMITER ;


Old question, but I ran into something similar. A workaround which solves the problem (although I am not 100% sure why), is the following:

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;

Some more info on what PREPARE expects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜