范例:
BEGIN
-- 定义参数
declare done int(10);
declare p_feeCode varchar(50);
declare abc varchar(50);
declare sqlstr varchar(500);
-- 定义游标
declare feeCodeCursor cursor for select TABLE_NAME FROM INFORMATION_SCHEMA.TABLES Where table_schema='common_db';
-- 定义变量
declare continue handler for not found set done=1;
set done=0;
-- 代开游标
open feeCodeCursor;
-- 执行循环
the_loop_new : LOOP
--
fetch feeCodeCursor into p_feeCode;
Set abc = concat("common_db.",p_feeCode);
SET sqlstr=concat("CREATE OR REPLACE view ",p_feeCode," as select * from ",abc,";");
set @v_sql=sqlstr;
prepare stmt from @v_sql;
EXECUTE stmt;
deallocate prepare stmt;
IF done = 1 THEN
close feeCodeCursor;
LEAVE the_loop_new;
END IF;
END LOOP the_loop_new;
END