juniorplsql
Programmer
Hello i am fairly new to plsql and i am tring to create a cursor that inserts a record into my employees table
This is the structure of my employees table:
EMP_ID NOT NULL NUMBER
DEPT_ID NOT NULL NUMBER
FIRST_NAME VARCHAR2(30)
LAST_NAME VARCHAR2(30)
INSERTED NOT NULL DATE
LAST_UPDATE NOT NULL DATE
HIRE_DATE NOT NULL DATE
JOB_ID NUMBER
SALARY NUMBER
COMM_PCT NUMBER
Here is my code:
1 DECLARE
2 v_emp_id employees.emp_id%TYPE;
3 v_dept_id employees.dept_id%TYPE;
4 v_inserted employees.inserted%TYPE;
5 v_last_update employees.last_update%TYPE;
6 v_hire_date employees.hire_date%TYPE;
7 CURSOR c_emp IS
8 SELECT EMP_ID, DEPT_ID, INSERTED, LAST_UPDATE, HIRE_DATE
9 FROM EMPLOYEES;
10 BEGIN
11 OPEN c_emp;
12 LOOP
13 FETCH c_emp INTO v_emp_id, v_dept_id, v_inserted, v_last_update, v_hire_date;
14 EXIT when c_emp%NOTFOUND;
15 INSERT INTO EMPLOYEES(emp_id, dept_id, inserted, last_update, hire_date)
16 VALUES(EMPLOYEES_SEQ.NEXTVAL, 10, SYSDATE, SYSDATE, '08-JUN-02');
17 END LOOP;
18 CLOSE c_emp;
19* END;
When I run it I get:
PL/SQL procedure successfully completed.
but when i do:
SQL> select * from employees;
no rows selected
I tried is this way but it inserted the records with the same data mulitple times:
DECLARE
CURSOR c_EMP_CURSOR IS
SELECT emp_id, dept_id, first_name, last_name, inserted, last_update, hire_date, job_id, salary, comm_pct
FROM EMPLOYEES;
emp_record c_EMP_CURSOR%ROWTYPE;
BEGIN
OPEN c_EMP_CURSOR;
LOOP
FETCH c_EMP_CURSOR INTO emp_record;
EXIT WHEN c_EMP_CURSOR%NOTFOUND;
INSERT INTO EMPLOYEES (emp_id, dept_id, first_name, last_name, inserted, last_update, hire_date, job_id, salary, comm_pct)
VALUES (EMPLOYEES_SEQ.NEXTVAL, 10, 'JOHN', 'SMITH', SYSDATE, SYSDATE, '10-JAN-02', 110, 7000, .10);
END LOOP;
CLOSE c_EMP_CURSOR;
END;
I combed in internet trying different things but cant get this to insert into my employees table. Could someone please show me the light.
This is the structure of my employees table:
EMP_ID NOT NULL NUMBER
DEPT_ID NOT NULL NUMBER
FIRST_NAME VARCHAR2(30)
LAST_NAME VARCHAR2(30)
INSERTED NOT NULL DATE
LAST_UPDATE NOT NULL DATE
HIRE_DATE NOT NULL DATE
JOB_ID NUMBER
SALARY NUMBER
COMM_PCT NUMBER
Here is my code:
1 DECLARE
2 v_emp_id employees.emp_id%TYPE;
3 v_dept_id employees.dept_id%TYPE;
4 v_inserted employees.inserted%TYPE;
5 v_last_update employees.last_update%TYPE;
6 v_hire_date employees.hire_date%TYPE;
7 CURSOR c_emp IS
8 SELECT EMP_ID, DEPT_ID, INSERTED, LAST_UPDATE, HIRE_DATE
9 FROM EMPLOYEES;
10 BEGIN
11 OPEN c_emp;
12 LOOP
13 FETCH c_emp INTO v_emp_id, v_dept_id, v_inserted, v_last_update, v_hire_date;
14 EXIT when c_emp%NOTFOUND;
15 INSERT INTO EMPLOYEES(emp_id, dept_id, inserted, last_update, hire_date)
16 VALUES(EMPLOYEES_SEQ.NEXTVAL, 10, SYSDATE, SYSDATE, '08-JUN-02');
17 END LOOP;
18 CLOSE c_emp;
19* END;
When I run it I get:
PL/SQL procedure successfully completed.
but when i do:
SQL> select * from employees;
no rows selected
I tried is this way but it inserted the records with the same data mulitple times:
DECLARE
CURSOR c_EMP_CURSOR IS
SELECT emp_id, dept_id, first_name, last_name, inserted, last_update, hire_date, job_id, salary, comm_pct
FROM EMPLOYEES;
emp_record c_EMP_CURSOR%ROWTYPE;
BEGIN
OPEN c_EMP_CURSOR;
LOOP
FETCH c_EMP_CURSOR INTO emp_record;
EXIT WHEN c_EMP_CURSOR%NOTFOUND;
INSERT INTO EMPLOYEES (emp_id, dept_id, first_name, last_name, inserted, last_update, hire_date, job_id, salary, comm_pct)
VALUES (EMPLOYEES_SEQ.NEXTVAL, 10, 'JOHN', 'SMITH', SYSDATE, SYSDATE, '10-JAN-02', 110, 7000, .10);
END LOOP;
CLOSE c_EMP_CURSOR;
END;
I combed in internet trying different things but cant get this to insert into my employees table. Could someone please show me the light.