How can we use cursor for a dynamic sql query?

I have a dynamic query being created in sql like this:

set sql_statement= concat( 'declare cursor_get_crm_org_metadata_ids cursor for select ’ , current_crm_org_uuid_name, ' into from ’ , current_crm_org_uuid_name, '.crm_erorr_metadata where ', current_crm_org_uuid_name , '.crm_erorr_metadata.object_error_count > 0 order by ', current_crm_org_uuid_name, ‘’);
set @sql= sql_statement;
prepare stmt from @sql;
execute stmt;

but i keep getting
Error Code: 1324. Undefined CURSOR: cursor_get_crm_org_metadata_ids

does anyone have an idea? The stackoverflow solutions don’t work at all.

Hi. This is not StackOverflow man. And why even bother to format your code for it to be at least somewhat readable, I guess?