views:

158

answers:

3

How can I dump out the body of a function or a procedure when using sqlplus to connect to an oracle database?

+1  A: 
select
    text
from
    user_source
where
    type = 'PROCEDURE'
and
    name='YOURPROCEDURENAME'
order by
    line;
dacracot
Likewise type='FUNCTION' and name='YOURFUNCTIONNAME'.
dacracot
We have several different schema set up. Do I to do anything special to deal with those?
David Oneill
You can either log into the specific schema, and user_source will only contain that schema's source. Or you can log in as a DBA and the dba_source table will also have an owner column which you can include in your where clause to specify which schema you are interested in.
dacracot
Actually, you want "select text". The LINE column has the line number.
Pop
Edited solution to select the right column
APC
Lol, thanks for the edit.
dacracot
+2  A: 

Use:

SELECT us.name,
       us.type,
       us.text
  FROM USER_SOURCE us
 WHERE us.type IN ('PROCEDURE', 'FUNCTION')
ORDER BY name, line
OMG Ponies
+2  A: 

Another solution is to use the *dbms_metadata* api

set line 200
set long 10000
select dbms_metadata.ddl('PACKAGE','Package Name') from dual;

You can use this for all metadata including tables, indexes and constraints.

Dinesh Bhat
Upvoting because those 'set line/set long' will be needed for the other mechanisms as well I think.
monojohnny