educative.io

Educative

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, '.crm_erorr_metadata.id 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, ‘.crm_erorr_metadata.id’);
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?


Course: https://www.educative.io/collection/5352985413550080/6335716377231360
Lesson: https://www.educative.io/collection/page/5352985413550080/6335716377231360/6003017074606080