Script to extract All DB objects from schema with proper file extension.

Is there any unix script or shell script  which can fetch all the DB objects from a particular schema along with the proper extension such as (.pks, .pkb etc) to my local system.
sam shahAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SujithData ArchitectCommented:
Here is something I wrote up. Assuming you are on UNIX/LINUX. You may tweak it as necessary

Save this file as spool_objects.sql. Modify the username passord and db name
CONNECT username/password@databasename
SET NEWPAGE 0
SET SPACE 0
SET LINESIZE 1000
SET TRIMSPOOL ON
SET PAGESIZE 0
SET ECHO OFF
SET FEEDBACK OFF
SET VERIFY OFF
SET HEADING OFF
SET TERMOUT OFF
SET MARKUP HTML OFF SPOOL OFF
SET ARRAYSIZE 500

SPOOL object_list.txt
select  object_name||','||CASE WHEN object_type = 'PACKAGE' THEN 'PACKAGE_SPEC' WHEN object_type = 'PACKAGE BODY' THEN 'PACKAGE_BODY' else object_type END||'
,'||user
from    user_objects
where   not regexp_like (object_type, 'PARTITION');
SPOOL OFF
EXIT;

Open in new window


Save this file as spool_code.sql. Modify the credentials
CONNECT username/password@databasename
SET NEWPAGE 0
SET SPACE 0
SET LINESIZE 1000
SET TRIMSPOOL ON
SET PAGESIZE 0
SET ECHO OFF
SET FEEDBACK OFF
SET VERIFY OFF
SET HEADING OFF
SET TERMOUT OFF
SET MARKUP HTML OFF SPOOL OFF
SET ARRAYSIZE 500
set long 90000;

SPOOL &1
select  dbms_metadata.get_ddl('&2', '&3', '&4')
from    dual;
SPOOL OFF
EXIT;

Open in new window


Save this file as gen_code.sh
#!/bin/bash
sqlplus /nolog << EOF
@spool_objects.sql
EOF

while read p; do
    IFS=','; array=($p); unset IFS;

    if [ "${array[1]}" == 'PACKAGE_SPEC' ]
    then
        filename="${array[0]}.pks"
    elif [ "${array[1]}" == 'PACKAGE_BODY' ]
    then
        filename="${array[0]}.pkb"
    else
        filename="${array[0]}.sql"
    fi

    echo "${filename}"

sqlplus /nolog << EOP
@spool_code.sql ${filename} ${array[1]} ${array[0]} ${array[2]}
EOP

done <object_list.txt

Open in new window


Change the mode of the shell script by issuing
chmod 755 gen_code.sh

run the shell script as
./gen_code.sh

It will generate one file each for your objects from the schema. pkb for package body. pks for spec. sql for rest.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sam shahAuthor Commented:
Thanks a lot.
0
SujithData ArchitectCommented:
Glad to help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.