w3resource

PL/SQL Control Statement Exercises: Insert records from one table to another

PL/SQL Control Statement: Exercise-29 with Solution

Write a program in PL/SQL to insert records from one table to another.

Sample Solution:

Table: employees

employee_id		integer
first_name		varchar(25)
last_name		varchar(25)
email			archar(25)
phone_number		varchar(15)
hire_date		date
job_id			varchar(25)
salary			integer
commission_pct		decimal(5,2)
manager_id		integer
department_id		integer

PL/SQL Code:

DROP TABLE emp_temp;
CREATE TABLE emp_temp (
  emp_id      NUMBER,
  emp_email  VARCHAR2(40)
);
 
DECLARE
  number_of_emp  NUMBER;
BEGIN
  SELECT COUNT(employee_id) INTO number_of_emp
  FROM employees;
  
  FOR i IN 1..number_of_emp LOOP
    INSERT INTO emp_temp (emp_id, emp_email)
    VALUES(i, 'not available now');
  END LOOP;
END;
/

Flowchart:

Flowchart: Insert records from one table to another.

Sample Output:

PL/SQL procedure successfully completed.

If you execute the command "select * from emp_temp;" you will see the data of emp_temp table.

Improve this sample solution and post your code through Disqus

Previous: Write a program in PL/SQL to check whether a number is prime or not using goto statement with for loop.
Next: Write a program in PL/SQL to insert a row if the featched value for a component is specified.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.