I am trying to solve this piece of code with a bulk insert or something else more effective but I am out of ideas. How would you approach this problem. Instead of looping so many times i would like to do it more effective in few calls. Please let me know how you would do? With code if possible! Thanx
LOOP
-- Fetch a row
IF DBMS_SQL.FETCH_ROWS(cursor_handle) > 0 THEN
DBMS_SQL.column_value(cursor_handle, 9, cont_id);
DBMS_SQL.COLUMN_VALUE(cursor_handle, 3, proj_nr);
HTP.BOLD('ContractID: ' || cont_id || ' ProjectNR: ' || proj_nr);
HTP.BR;
ELSE
EXIT;
END IF;
-- delete the old list before saving a new one
IF sek_nr = 1 THEN
EXECUTE IMMEDIATE 'DELETE FROM W_Contracts WHERE user_id = :n' USING CURRENTUSER;
END IF;
EXECUTE IMMEDIATE 'Insert into W_Contracts values(''' || currentUser || ''', '
|| sek_nr || ', sysdate, ' || cont_id || ', '''
|| proj_nr || ''')';
sek_nr := sek_nr + 1;
END LOOP;
First off, it’s not clear to me why you’re using dynamic SQL rather than static SQL.
Next, it’s not obvious to me why you’re potentially doing a
DELETEand then anINSERTif theSEK_NRis 1. It would likely be more efficient to do anUPDATEin that case. And once you’re doing anUPDATEand anINSERT, you can simplify that into a singleMERGEstatement.Next, if you use the
DEFINE_ARRAYmethod inDBMS_SQL, you can do bulk fetches of data from your cursor. Of course, without seeing your cursor definition, I would be suspicious that it too was using dynamic SQL unnecessarily and that you could use a much simpler approach.