Reference: Write a MySQL stored procedure for dynamic execution of SQL, or you can use temporary tables instead of views.
Drop procedure if existsP_simulate_dynamic_cursor;Create procedurep_simulate_dynamic_cursor ()begin DeclareV_sqlvarchar(4000); DeclareV_fieldvarchar(4000); DeclareV_resultvarchar(4000)default "'; DeclareCur_tempcursor for SelectV.* fromview_temp_20150701 v; Declare ContinueHandler for notFoundSetV_field= NULL; SetV_sql= 'CREATE View view_temp_20150701 as select T.id from T_user t'; Set @v_sql =V_sql; PrepareStatement from @v_sql; Executestatement; deallocate Preparestatement; Opencur_temp; FetchCur_temp intoV_field; while(V_field is not NULL) doSetV_result=Concat (V_result, V_field,','); FetchCur_temp intoV_field; End while; Closecur_temp; SelectV_result; Drop View if existsview_temp_20150701;End; call P_simulate_dynamic_cursor ();
MySQL plsql Demo-004. Simulate dynamic cursors