问题
I need to extract code(script) from all my functions, procedures, packages, views and tables, so that when I move to production I could run the script to create all the objects.
While developing I did not take script backup of all database objects.
What is the best way to extract code or script? Any suggestion or help is highly appreciable.
Thanks
回答1:
You do use a version control system don't you? Please do.
Failing that you can use the system function dbms_metadata.get_ddl, which will return a clob of the DDL used to create the object. This will need to be done for each individual object so it might be worth looping through user_objects.
Alternatively you can go through the definitions in PL/SQL Developer by right-clicking on an object and using the view
option then in the bottom right corner you can view the SQL used to create the object.
There is also an export "tool", which doesn't have as good an explanation of how to use it as the SQL Developer variant.
Of course, the correct answer is get a version control system and use it.
回答2:
I don't know about the database itself, but SQL Developer will do it. I'm pretty sure TOAD and other such tools will as well.
回答3:
Sorry but you have a non-optimal development practice that doesn't scale beyond one developer.
Never use SQL GUI clients' abilities to edit database objects directly in the database. (Unless you deliberately want to create artificial problems.)
Instead all SQL and PL/SQL DDL and DML should be located in text files saved into a version control system (VCS) (e.g. subversion, git, mercurial). Use your favorite editor to edit the files.
Modify the database by executing the text files saved in VCS with a command line tool (in Oracle that's sqlplus
). This applies to all database instances: development, QA, production.
For large development projects consider using schema migration tools (e.g. Flyway, Roundhouse).
Over the years I have been able to eliminate tons of development and production issues by enforcing a simple rule:
If the code is not in the VCS it doesn't exists.
(It's amazing that still in 2015 there's many development teams not using a VCS.)
回答4:
In my case, all the custom objects start with 'XX'. You could use or modify my script:
DECLARE
CURSOR c_tables IS
SELECT replace(object_type, ' ', '_') type,
object_name name,
owner schema,
object_type||'#'||object_name||'.sql' files
FROM all_objects
WHERE UPPER(object_name) LIKE 'XX%'
AND object_type NOT IN ('DIRECTORY');
ddl_text CLOB;
BEGIN
FOR rec_tables IN c_tables LOOP
dbms_output.put_line('OBJECT_TYPE: '||rec_tables.type||', OBJECT_NAME: '||rec_tables.name||', SCHEMA: '||rec_tables.schema||' ->'||rec_tables.files);
SELECT dbms_metadata.get_ddl(rec_tables.type, rec_tables.name, rec_tables.schema)
INTO ddl_text
FROM DUAL;
dbms_xslprocessor.clob2file(ddl_text, 'UTL_CUSTOM_DDL', rec_tables.files, nls_charset_id('WE8MSWIN1252'));
END LOOP;
END;
Regards
来源:https://stackoverflow.com/questions/10946666/extract-code-or-script-of-database-objects