KB · Oracle Database · Script

Extract PL/SQL source to a file from dba_source

Oracle Database · Script · Verified on Oracle 11.2 and 23ai · Updated 2026-12-03

When you need the code that's actually deployed (to diff it against version control, or to patch production in a hurry), this writes one PL/SQL unit from dba_source into a file you can run as-is: CREATE OR REPLACE in front, / at the end.

Usage

SQL> @src.sql HR PAYROLL_PKG "PACKAGE BODY"

It writes HR.PAYROLL_PKG.sql in the current directory. The type is one of PACKAGE, PACKAGE BODY, PROCEDURE or FUNCTION.

src.sqlMIT · Download ·
-- src.sql: spool a package, package body, function or procedure to a file
-- Copyright (c) HASHJOIN. MIT License (https://hashjoin.com/kb/license/)
-- https://hashjoin.com/kb/oracle/extract-plsql-source/
--
-- Usage: @src.sql <owner> <name> <type>
--        @src.sql HR PAYROLL_PKG "PACKAGE BODY"     writes HR.PAYROLL_PKG.sql

set scan on
set pages 0
set feedback off
set heading off
set verify off
set lines 300
set trims on

set term on

set term off
set echo off

SELECT DECODE(ROWNUM,1,'CREATE OR REPLACE '||text,text)
FROM   dba_source
WHERE  name  = UPPER('&&2')
AND    owner = UPPER('&&1')
AND    type  = UPPER('&&3')
ORDER BY line

spool &&1..&&2..sql
/
prompt /
spool off

undefine 1
undefine 2 
undefine 3

curl -O https://hashjoin.com/kb/oracle/extract-plsql-source/src.sql

Gotchas

What it reads

dba_source

Tired of fixing the same thing twice?

HASHJOIN turns the checks and fixes your on-call runs by hand into tooling that gives the same answer every time. Fixed scope, and you own all of the code.