Mysql存儲過程處理動態參數 Sql代碼 --刪除 drop procedure if exists up_common_select --創建 CREATE PROCEDURE `up_common_select` ( in t_name varchar(50) ) begin declare v_sql varchar(500); set v_sql= concat('select * from ',t_name); select v_sql; --注意:prepare(預處理)execute stmt using @var,只能跟@var變量,declare和傳入的變量不行!!! set @v_sql=v_sql; prepare stmt from @v_sql; EXECUTE stmt ; deallocate prepare stmt; end; --調用 call up_common_select('admin_authority'); 注意事項 1 mysql5.0.13之後支持在存儲過程中調用prepare 2 prepare stmt from 'select * from ?'; (錯) mysql5.0.24,prepare尚不支持 表名做變量! 解決方案:用 contat()函數,組合字符串 3 execute stmt [using @var,@var2] 必須是@var形式的變量,傳入的參數變量,declare變量不行 4. deallocate prepare stmt; 顯式的釋放prepare,如果不釋放,mysql會釋放,!