Generate INSERT statements from the SELECT query

How to Generate INSERT statements from the SELECT query


Generate INSERT statements from the SELECT query

-- Set the output file path and name
SPOOL /path/to/output_file.sql

-- Generate INSERT statements from the SELECT query
SET PAGESIZE 0
SET HEADING OFF
SET FEEDBACK OFF

-- You can customize the column list and WHERE condition as needed
SELECT 'INSERT INTO target_table (col1, col2, col3) VALUES (' ||
       CHR(39) || col1 || CHR(39) || ', ' ||
       CHR(39) || col2 || CHR(39) || ', ' ||
       CHR(39) || col3 || CHR(39) || ');'
FROM source_table
-- WHERE some_condition;

-- Turn off SPOOL
SPOOL OFF


Test Output

SQL> SELECT * FROM EMP WHERE JOB='CLERK';

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20
      7876 ADAMS      CLERK           7788 23-MAY-87       1100                    20
      7900 JAMES      CLERK           7698 03-DEC-81        950                    30
      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10

SQL>

[oracle@br8dba01 ~]$ cat 1.sql
SET PAGESIZE 0
SET HEADING OFF
SET FEEDBACK OFF
SPOOL output_file.sql
SELECT 'INSERT INTO EMP (EMPNO, ENAME, JOB,MGR, HIREDATE, SAL, COMM, DEPTNO) VALUES (' ||
       CHR(39) || EMPNO || CHR(39) || ', ' ||
       CHR(39) || ENAME || CHR(39) || ', ' ||
       CHR(39) || JOB || CHR(39) || ', ' ||
           CHR(39) || MGR || CHR(39) || ', ' ||
           CHR(39) || HIREDATE || CHR(39) || ', ' ||
           CHR(39) || SAL || CHR(39) || ', ' ||
           CHR(39) || COMM || CHR(39) || ', ' ||
           CHR(39) || DEPTNO || CHR(39) || ');'
FROM EMP WHERE JOB='CLERK';
SPOOL OFF
[oracle@br8dba01 ~]$

SQL> @1.sql
INSERT INTO EMP (EMPNO, ENAME, JOB,MGR, HIREDATE, SAL, COMM, DEPTNO) VALUES ('73
69', 'SMITH', 'CLERK', '7902', '17-DEC-80', '800', '', '20');

INSERT INTO EMP (EMPNO, ENAME, JOB,MGR, HIREDATE, SAL, COMM, DEPTNO) VALUES ('78
76', 'ADAMS', 'CLERK', '7788', '23-MAY-87', '1100', '', '20');

INSERT INTO EMP (EMPNO, ENAME, JOB,MGR, HIREDATE, SAL, COMM, DEPTNO) VALUES ('79
00', 'JAMES', 'CLERK', '7698', '03-DEC-81', '950', '', '30');

INSERT INTO EMP (EMPNO, ENAME, JOB,MGR, HIREDATE, SAL, COMM, DEPTNO) VALUES ('79
34', 'MILLER', 'CLERK', '7782', '23-JAN-82', '1300', '', '10');

SQL>

[oracle@br8dba01 ~]$ cat output_file.sql
INSERT INTO EMP (EMPNO, ENAME, JOB,MGR, HIREDATE, SAL, COMM, DEPTNO) VALUES ('73
69', 'SMITH', 'CLERK', '7902', '17-DEC-80', '800', '', '20');

INSERT INTO EMP (EMPNO, ENAME, JOB,MGR, HIREDATE, SAL, COMM, DEPTNO) VALUES ('78
76', 'ADAMS', 'CLERK', '7788', '23-MAY-87', '1100', '', '20');

INSERT INTO EMP (EMPNO, ENAME, JOB,MGR, HIREDATE, SAL, COMM, DEPTNO) VALUES ('79
00', 'JAMES', 'CLERK', '7698', '03-DEC-81', '950', '', '30');

INSERT INTO EMP (EMPNO, ENAME, JOB,MGR, HIREDATE, SAL, COMM, DEPTNO) VALUES ('79
34', 'MILLER', 'CLERK', '7782', '23-JAN-82', '1300', '', '10');

[oracle@br8dba01 ~]$

Caution: Your use of any information or materials on this website is entirely at your own risk. It is provided for educational purposes only. It has been tested internally, however, we do not guarantee that it will work for you. Ensure that you run it in your test environment before using.

Thank you,
Rajasekhar Amudala
Email: br8dba@gmail.com
Linkedin: https://www.linkedin.com/in/rajasekhar-amudala/