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.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 3curl -O https://hashjoin.com/kb/oracle/extract-plsql-source/src.sql
Gotchas
- A package spec and body go to the same file name, so extract the spec, rename the file, then extract the body.
- Wrapped (obfuscated) code comes out wrapped.
- For other object types, or to include grants and storage, use
dbms_metadata.get_ddlinstead.
What it reads
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.
New fixes and field notes by email, about once a month.