1

When using Oracle's dbms_metadata.get_ddl for getting DDL I see some extra characters, especially there are whitespace characters at very beginning of DDL script. For example, if I create this sample procedure:

CREATE OR REPLACE PROCEDURE TEST
IS 
BEGIN
 NULL;
END;
/ 

then dbms_metadata.get_ddl for this procedure returns extra newline and two space characters at very beginning:

select dbms_lob.substr(dbms_metadata.get_ddl('PROCEDURE','TEST', USER), 37, 1 ) text
, dump(dbms_lob.substr(dbms_metadata.get_ddl('PROCEDURE','TEST', USER), 37, 1 )) dump_text
FROM dual;

Result is:
  CREATE OR REPLACE PROCEDURE "TEST"                                            
Typ=1 Len=37: 10,32,32,67,82,69,65,84,69,32,79,82,32,82,69,80,76,65,67,69,32,80,
82,79,67,69,68,85,82,69,32,34,84,69,83,84,34

As you can see there are 10,32,32,, just before CREATE OR REPLACE. Any idea how to get rid of these whitespaces? Also there are extra " around procedure name.

3
  • 2
    The double quotes are there to allow for the possibility that you created the procedure using a reserved word or containing a non-standard character. Commented May 10, 2016 at 10:22
  • Looking at the comments below and here, I think that you may be trying to do something in a slightly backwards fashion. Can you explain what it is you want to achieve, then someone may be able to provide a better way to manage it. Commented May 11, 2016 at 11:57
  • I am guessing you may not be able to directly get rid of those 3 characters and double-quotes easily. These probably get added when xml to ddl transformation happens (dbms_metadata first generates xml and transforms it to ddl or sxml if requested). Because if you use dbms_metadata.get_xml, you don't see those characters, but you see them for get_ddl(). a substr(...get_ddl(),4) could fix that. You have to be careful about double quotes though.
    – Raj
    Commented May 18, 2016 at 18:44

3 Answers 3

1

Please try this:

set long 99999999
set pages 1000
exec dbms_metadata.set_transform_param(dbms_metadata.session_transform, 'PRETTY', true);
exec dbms_metadata.set_transform_param(dbms_metadata.session_transform, 'SQLTERMINATOR', true);
select dbms_metadata.get_ddl('<object_type>','<object_name>','<owner>') from dual;
1

The following will get rid of the LF and 2 spaces using a function though it would probably be better/easier to do it in your text editor.

select 
   REPLACE(dbms_lob.substr(
        dbms_metadata.get_ddl('FUNCTION','REMAP_SCHEMA', 'ETL'), 
      37, 1), CHR(10)||CHR(32)||CHR(32)||'CREATE','CREATE') text
   from dual

Obviously the first word that you are interested in keeping would need to be 'CREATE' for this to work. You could also use one of the REGEXP functions but I don't see that being any better/different to this method. I'm also not sure why you particularly need to get rid of them.

2
  • I need to get exactly the same source code as it was created from. It will be compared semi-automatically externally for modifications, source versioning etc. I have the same thing for SQL Server and a lot of code. In Oracle, there would work user_source, but I look for solution with dbms_metadata. Commented May 10, 2016 at 11:27
  • You may need to get the first line using the above but wrap it in another REPLACE that replaces q'["]' with NULL. Commented May 10, 2016 at 13:03
0

code as is, without any transformations or formatting:

  function getDDL(
      p_object_type varchar2
     ,p_object_owner varchar2
     ,p_object_name varchar2
  ) return clob is
      v_clob clob;
  begin     
      dbms_lob.createtemporary(
          lob_loc => v_clob
         ,cache   => true
         ,dur     => DBMS_LOB.CALL
      );
      for rec in (
          SELECT text
          FROM ALL_SOURCE
          WHERE NAME  = p_object_name
            AND OWNER = p_object_owner
            and type  = p_object_type
          order by line  
      ) loop
          DBMS_LOB.append(v_clob, rec.text);
      end loop;
      return v_clob;
  end getDDL;
1
  • Please consider adding an explanation why you think this piece of code does what the question author wants; code-only answers are not very helpful.
    – mustaccio
    Commented Aug 15, 2022 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.