MySql 存储过程 表名作为动态参数传入

MySql 存储过程 表名作为动态参数传入

DROP PROCEDURE IF EXISTS info_bak;
CREATE PROCEDURE info_bak(IN talbeName VARCHAR(30), OUT retMsg VARCHAR(30))
BEGIN
	DECLARE v_err INT DEFAULT 0;
	DECLARE v_msg VARCHAR(30);
	DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_err  = 1;
	DECLARE CONTINUE HANDLER FOR SQLWARNING SET v_err = 2;
	DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET v_err = 3;
	SET @sqlcmd = CONCAT('REPLACE INTO infobak SELECT * FROM ',talbeName,' ;');
	PREPARE stmt FROM @sqlcmd;
	EXECUTE stmt;
	DEALLOCATE PREPARE stmt;
	IF v_err = 1 THEN
		SET v_msg = "Not Found";
	ELSEIF v_err = 2 THEN
		SET v_msg = "Warning";
	ELSEIF v_err = 3 THEN
		SET v_msg = "Error";
	ELSE 
		SET v_msg = "OK";
	END IF;
	COMMIT;
	SET retMsg = v_msg;
END;

CALL info_bak('info', @msg);
SELECT @msg;

 

你可能感兴趣的:(MySql 存储过程 表名作为动态参数传入)