The following query needs to convert to dynamic SQL without hard code cursor SQL, using l_query, I do not know the l_query it will come as a parameter. Inside the loop, I need to execute another insert query ( l_insert_query) that also comes as a parameter.
Your counsel would be much appreciated
DECLARE
CURSOR cust
IS
SELECT *
FROM customer
WHERE id < 500;
BEGIN
l_query := 'SELECT * FROM customer WHERE id < 5';
l_insert_query :=
'insert into data ( name, mobile) values ( cust.name,cust.mobile)';
FOR r_cust IN cust
LOOP
EXECUTE IMMEDIATE l_insert_query;
END LOOP;
END;
l_insert_queryis supposed to reference thenamecolumn from thecustomertable inl_query? If you know nothing aboutl_query, you have no idea how many tables it is querying from or what columns it returns let alone what alias a human might choose for the table name.l_queryandl_insert_queryis parameter, It seems that your problem involve to semantic analysis of those two parameter. First you should build an algorithm to read those two parameter, check if both is valid SQL query, andl_queryis suitable to runl_insert_query. With that algorithm, you could do whateverl_insert_querywant to do, using dynamic SQL or maybe only SQL is enough.l_insert_querywill always reference columns ascust.<<column name>>? Why would you design a system this way? Ifl_insert_querywas justinsert into data( name, mobile )andl_querywasselect name, mobile from customer where id < 5, you could at least concatenate the two to generate theinsertstatement dynamically.dbms_sqlto executel_query, dynamically interrogate the cursor handle to get a list of column names, searchl_insert_queryfor anycust.references, replace those with bind variables, iterate over thedbms_sqlcursor handle callingl_insert_queryand passing in bind variables. That's a whole pile of rather pointless string manipulation anddbms_sqlis not a trivial package to work with (and debugging and maintenance are going to be much harder).