How to use for loop for insert in oracle?

Member

by jerad , in category: MySQL , 2 months ago

How to use for loop for insert in oracle?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by elisha_langworth , 2 months ago

@jerad 

To use a for loop for inserting data in an Oracle database, you can follow the steps below:

  1. Create a PL/SQL block with a FOR loop to iterate over a list of values that you want to insert into the database.
  2. Within the FOR loop, use the INSERT statement to insert each value into the database table.
  3. Here is an example of how you can use a FOR loop for inserting data in Oracle:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
DECLARE
   -- Declare variables
   v_name VARCHAR2(50);
BEGIN
   -- Create a list of values to be inserted
   FOR i IN 1..5 LOOP
      -- Assign a value to the variable
      v_name := 'Name ' || i;
      
      -- Insert the value into the database table
      INSERT INTO your_table_name (column_name) VALUES (v_name);
   END LOOP;
END;
/


  1. Replace your_table_name with the actual name of the table you want to insert data into and column_name with the name of the column in which you want to insert the values.
  2. Execute the PL/SQL block in Oracle SQL*Plus or any other SQL client to insert the data using the FOR loop.


This is a simple example of how you can use a FOR loop for inserting data in an Oracle database. You can modify the code to suit your specific requirements and insert more complex data as needed.