Generating DDL script for object without schema name baked in using DBMS_METADATA.GET_DDL?
Asked Answered
H

3

10

How can I generate the DDL script for my object with DBMS_METADATA.GET_DDL without the schema name baked in?

With DBMS_METADATA.GET_DDL:

CREATE TABLE "MYSCHEMA"."MYTABLE"
(
  "COL1"  NUMBER(10,0)
)

SQL Developer can do that, and I think it's also uses the DBMS_METADATA to achive this goal and generale DDL scripts.

With SQL Developer:

CREATE TABLE "MYTABLE"
(
  "COL1"  NUMBER(10,0)
)
Hailstone answered 21/7, 2011 at 20:23 Comment(2)
Here's one example... myoraclesupports.com/content/…Pushkin
The example link no longer works, here is an archived versionMensurable
N
8

Use SET_REMAP_PARAM with the REMAP_SCHEMA option:

DBMS_METADATA.SET_REMAP_PARAM(th,'REMAP_SCHEMA','HR',NULL);

This will map the HR schema to NULL (you'll need a job handle, though); for a full example, see metadata_api documentation

Norvun answered 21/7, 2011 at 20:44 Comment(1)
This answer did not work for me. The answer with EMIT_SCHEMA worked though.Mistrust
S
22

I recently stumbled upon the following which allows you to get ddl without the schema name.

It looks a lot simpler than any other way I have seen so far although its not included in any Oracle documentation. I spotted it in the Statements Log in SQL Developer, which generates ddl without the schema name.

DBMS_METADATA.SET_TRANSFORM_PARAM(dbms_metadata.SESSION_TRANSFORM, 'EMIT_SCHEMA', false);

You don't need to get handles or anything nasty just EXEC the above before calling DBMS_METADATA.GET_DDL

Scandic answered 30/7, 2015 at 13:48 Comment(2)
Nice find, easier / more straightforward than my solution!Norvun
This worked for me but the accepted answer did not. (I am using oracle 19.)Mistrust
N
8

Use SET_REMAP_PARAM with the REMAP_SCHEMA option:

DBMS_METADATA.SET_REMAP_PARAM(th,'REMAP_SCHEMA','HR',NULL);

This will map the HR schema to NULL (you'll need a job handle, though); for a full example, see metadata_api documentation

Norvun answered 21/7, 2011 at 20:44 Comment(1)
This answer did not work for me. The answer with EMIT_SCHEMA worked though.Mistrust
R
1

In addition to removing schema(As suggested by David Norris above), if you want to remove storage and other attributes. Basically, just a plain DDL, then use below :

BEGIN
DBMS_METADATA.set_transform_param (DBMS_METADATA.session_transform, 'SQLTERMINATOR', TRUE);
DBMS_METADATA.set_transform_param (DBMS_METADATA.session_transform, 'PRETTY', TRUE);
DBMS_METADATA.set_transform_param (DBMS_METADATA.session_transform, 'SEGMENT_ATTRIBUTES', FALSE);
DBMS_METADATA.set_transform_param (DBMS_METADATA.session_transform, 'STORAGE', FALSE);      
DBMS_METADATA.set_transform_param (DBMS_METADATA.session_transform, 'EMIT_SCHEMA', FALSE);
END;
Riordan answered 4/5, 2020 at 21:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.