Press "Enter" to skip to content

Oracle Dynamic SQL Example to Insert a Record Using DBMS_SQL

In this tutorial, I am giving an Oracle dynamic SQL example to insert a record using the DBMS_SQL package.

Through dynamic SQL, you can parse any DML and DDL statement using PL/SQL to execute to perform a particular task in Oracle Database.

Oracle Dynamic SQL Example: Insert New Record Using DBMS_SQL

The following PL/SQL procedure will insert a new record into the EMPLOYEES table of the HR schema. A string variable “V_SQL” is used for insert statement, so that it could be manipulated and parse using the DBMS_SQL.  The stored procedure accepts seven parameters which will be used to bind using the DBMS_SQL.BIND_VARIABLE method. Also giving the script for table and sequence object so that you can test locally.

Create EMPLOYEES Table

CREATE TABLE EMPLOYEES
(
   EMPLOYEE_ID      NUMBER (6),
   FIRST_NAME       VARCHAR2 (20 BYTE),
   LAST_NAME        VARCHAR2 (25 BYTE) NOT NULL,
   EMAIL            VARCHAR2 (25 BYTE) NOT NULL,
   PHONE_NUMBER     VARCHAR2 (20 BYTE),
   HIRE_DATE        DATE NOT NULL,
   JOB_ID           VARCHAR2 (10 BYTE) NOT NULL,
   SALARY           NUMBER (8, 2),
   COMMISSION_PCT   NUMBER (2, 2),
   MANAGER_ID       NUMBER (6),
   DEPARTMENT_ID    NUMBER (4)
)
/

Create a Sequence Object

CREATE SEQUENCE employee_seq
   START WITH 1
   INCREMENT BY 1
   ORDER
/

Create Stored Procedure

CREATE OR REPLACE PROCEDURE new_employee (i_FIRST    IN VARCHAR2,
                                          i_LAST     IN VARCHAR2,
                                          i_email    IN VARCHAR2,
                                          i_phone    IN VARCHAR2,
                                          i_hired    IN DATE,
                                          i_job      IN VARCHAR2,
                                          i_deptno   IN NUMBER DEFAULT 0)
AS
   v_sql           VARCHAR2 (1000);

   cursor_var      NUMBER := DBMS_SQL.OPEN_CURSOR;
   rows_complete   NUMBER := 0;
   next_emp_id     NUMBER := employee_seq.NEXTVAL;
BEGIN
   IF i_deptno != 0
   THEN
      v_sql :=
            'INSERT INTO EMPLOYEES ( '
         || 'employee_id, first_name, last_name, email, '
         || 'phone_number, hire_date, job_id, department_id) '
         || 'VALUES( '
         || ':next_emp_id, :first, :last, :email, :phone, :hired, '
         || ':job_id, :dept)';
   ELSE
      v_sql :=
            'INSERT INTO EMPLOYEES ( '
         || 'employee_id, first_name, last_name, email, '
         || 'phone_number, hire_date, job_id) '
         || 'VALUES( '
         || ':next_emp_id, :first, :last, :email, :phone, :hired, '
         || ':job_id)';
   END IF;

   DBMS_SQL.PARSE (cursor_var, v_sql, DBMS_SQL.NATIVE);
   DBMS_SQL.BIND_VARIABLE (cursor_var, ':next_emp_id', next_emp_id);
   DBMS_SQL.BIND_VARIABLE (cursor_var, ':first', i_FIRST);
   DBMS_SQL.BIND_VARIABLE (cursor_var, ':last', i_LAST);
   DBMS_SQL.BIND_VARIABLE (cursor_var, ':email', i_email);
   DBMS_SQL.BIND_VARIABLE (cursor_var, ':phone', i_phone);

   DBMS_SQL.BIND_VARIABLE (cursor_var, ':hired', i_hired);

   DBMS_SQL.BIND_VARIABLE (cursor_var, ':job_id', i_job);

   IF i_deptno != 0
   THEN
      DBMS_SQL.BIND_VARIABLE (cursor_var, ':dept', i_deptno);
   END IF;

   rows_complete := DBMS_SQL.EXECUTE (cursor_var);
   DBMS_SQL.CLOSE_CURSOR (cursor_var);
   COMMIT;
END;
/

Test

DECLARE
   I_FIRST    VARCHAR2 (32767);
   I_LAST     VARCHAR2 (32767);
   I_EMAIL    VARCHAR2 (32767);
   I_PHONE    VARCHAR2 (32767);
   I_HIRED    DATE;
   I_JOB      VARCHAR2 (32767);
   I_DEPTNO   NUMBER;
BEGIN
   I_FIRST := 'Kevin';
   I_LAST := 'John';
   I_EMAIL := '[email protected]';
   I_PHONE := '2299378';
   I_HIRED := SYSDATE;
   I_JOB := 'CLERK';
   I_DEPTNO := 10;

   NEW_EMPLOYEE (I_FIRST,
                        I_LAST,
                        I_EMAIL,
                        I_PHONE,
                        I_HIRED,
                        I_JOB,
                        I_DEPTNO);
END;

Result

PL/SQL program Test result.

See also: