Recently, I encountered a problem in writing the stored procedure. I always encountered an error when using batch data insertion. It was said that the uniqueness constraint was violated. Finally, I checked the stored procedure and found that the type table data was not deleted. The stored procedure is as follows:
- Type type_char2 is table of NVARCHAR2 (30) index by binary_integer; -- defines the temporary table type of a string
- V_card_id type_char2;
- -- The following is a cursor.
- Cursor cur_bt_data is
- Select * from test ....;
- -- Traversal cursor
- For bt_row in cur_bt_data loop
- For I in 1 .. bt_row.confirm_quanlity loop
- V_card_id (I):=To_number(Bt_row.iccid_start) + I-1;
- End loop;
- Forall I in 1 .. v_card_id.count
- Insert/* + append */
- Into demo
- (Card_id ,....)
- Values
- (V_card_id (I ),...);
- Commit;
- End loop; -- [END] for 'cur _ bt_data'
V_card_id (I) in the query is found. If the number of bt_row.confirm_quanlity queried is the same, the data should be reinitialized. If the number is different, for example, the number of the previous query is large, the number of the next time is small. If v_card_id is not initialized, duplicate results may appear. All temporary tables must be cleared in each loop.
After checking the relevant information, you only need to useV_card_id.deleteDelete the temporary table.
Modify as follows:
- For bt_row in cur_bt_data loop
- V_card_id.delete;
- For I in 1 .. bt_row.confirm_quanlity loop
- ..........