Never in DBA history has there been a better time than now. There are so many GUI tools available for DBAs to use, free and paid, it is hard to even choose from.
One might think that the use of DBMS_METADATA package is obsolete, or old school. Before completely
dismissing the idea of using this package think again.
It seems very easy to open up Toad, SQL Developer or a GUI tool of your choice, and just click through the menu to get to the object you are interested in. Retrieving the object DDL this way is totally fine.
Imagine working for a client that has no GUI tools. (I know this sounds disturbing for many). You need to use SQLPLUS.
This is actually reality, there are clients who do not
have GUI tools available for DBAs to use, and DBAs are expected to deliver.
So what do you do, if you need to get the DDL for a table, or a user, or any object?
You do your DBA Magic. Pull out your script and say the magic words: DBMS_METADATA.
Let’s explore the following today:
1.When To Use DBMS_METADATA?
2.Why Use DBMS_METADATA?
3.How To Use DBMS_METADATA?
1.When To Use DBMS_METADATA?
Use DBMS_METADATA anytime you need to extract the DDL of an object, of a user or role.
Let me give you an example: you are changing the password for an application schema in the
database. Prior to the password change you want to backup the user’s encrypted password. One way to do this and script it is to extract the user DDL, with DBMS_METADATA.
Use DBMS_METADATA during database refreshes, to extract permissions, users’ passwords and other things, prior to the refresh. The generated scripts can be applied after the refresh, to preserve permissions, passwords and other settings.
Use DBMS_METADATA during database upgrades, if you upgrade
using export – import method, to port over users, roles, permissions and other things.
Use DBMS_METADATA when you need to backup certain objects that are going to be changed or dropped.
Some of you might think it is not your responsibility to backup object DDL.
Remember my mantra: “A Good DBA Never Assumes“?
Do not assume that others have backups of object DDL, even if they have backups, might not be the latest version.
2.Why Use DBMS_METADATA?
My favorite three reasons to use DBMS_METADATA:
- fast, especially if you have your script ready for extraction. No need to click through GUI tools and wait until the objects load.
- no special software is required. You only need to have SQLPLUS. It is 100% guaranteed SQLPLUS is always available on the database server, no matter what.
- you can script the DDL extraction, automate it, spool the results. It is less prone to errors, more
prone to success.
3.How To Use DBMS_METADATA
I will not go through all the subprograms and requirements of DBMS_METADATA package. You can ready through Oracle documentation for that purpose.
A good script that extract object DDL, needs to have
the following features:
– be dynamic, can be run for different objects, without making changes to the script
– output of the script needs to be clean, readable, aligned, without split lines.
– generated statements need to be terminated with “;”
– no heading and no feedback
– generated statements can be run without any errors in the database.
I’ll share with you two of the scripts I use: get_table_ddl.sql, to get table and
associated indexes ddl, and get_user_ddl.sql to get the user create statements and granted roles and permissions.
You get the idea on how to use this package and create your scripts.
SQL>@get_table_ddl.sql OWNER TABLE_NAME