Mysql中的Dynamic sql,相似於oracle中的Execute immediately語法,寫法上稍微麻煩一點,下面是一個具體的例子。sql
DROP PROCEDURE IF EXISTS total_num; CREATE PROCEDURE total_num( IN p_begin INT, IN p_end INT ) BEGIN DECLARE v_begin INT DEFAULT 0; DECLARE v_end INT DEFAULT 0; DECLARE v_total INT DEFAULT 0; SET v_begin = p_begin; SET v_end = p_end; WHILE v_begin<=v_end DO SET @sql = CONCAT('SELECT COUNT(1) INTO @item_count FROM t', v_begin); PREPARE stmt FROM @sql; EXECUTE stmt; SET v_total = v_total + @item_count; SET v_begin = v_begin+1; END WHILE; DEALLOCATE PREPARE stmt; SELECT v_total; END
調用方式:oracle
call total_num(1,2)