ORACLE How to use spool with dynamic spool location

后端 未结 3 1570
旧时难觅i
旧时难觅i 2020-12-19 13:58

Ok, so i\'m a complete newb with oracle. Now that that\'s out of the way;

I think you can get an understand of what i\'m trying to do below. For each stored procedur

相关标签:
3条回答
  • 2020-12-19 14:27

    SPOOL is a SQLPlus directive and you can't mix it into the PL/SQL anonymous block. If you're going to do this purely in SQLPlus, I think the general idea would be to process in two passes, i.e. use a first script that dynamically generates the spool filename references into a second script that actually makes the dbms_metadata call.

    [Edit]

    This should be close to what you need - maybe a line termination problem, depending on your platform:
    
        set pagesize 0
        set linesize 300
        spool wrapper.sql
        select
        'spool '||object_name||'.sql'||chr(10)||
        'begin 
        dbms_metadata.get_ddl('||chr(39)||object_type||chr(39)||','||chr(39)||object_name||chr(39)||')'||' end;'||chr(10)||
        '/'||chr(10)||
        'spool off'
        from user_objects
        where object_type = 'PROCEDURE'
    ;
    spool off
    
    0 讨论(0)
  • 2020-12-19 14:31

    I think UTL_FILE would be much better suited for your needs here. SPOOL is really supposed to be a command to instruct sqlplus to write the output to a file. Typically I use this for stuff like ... "hey DBA run my script and send me the output".

    First you need to define a directory. Syntax is easy:

    
    CREATE DIRECTORY SQLOUTPUT AS 'c:\temp\';
    

    Now you can use that in your code:

    
    DECLARE
    
      -- Get all procedure from All_Objects
      -- You could expand this to pass in the object_type you are looking for
      CURSOR csr IS
        SELECT object_type
          , object_name
        FROM All_Objects
        WHERE object_type = 'PROCEDURE'
        AND owner = 'AMS';
    
      -- define a file handler type
      outputfile UTL_FILE.file_type;
    
    BEGIN
    
    FOR c IN csr LOOP
    
      -- open your file using the procedure name from the cursor
      outputfile := UTL_FILE.fopen('SQLOUTPUT',c.object_name||'.prc.sql','W');
    
      -- output the metadata results just like DBMS_OUTPUT except to a file
      UTL_FILE.put_line(outputfile, DBMS_METADATA.get_ddl(c.object_type, c.object_name));
    
      -- make sure to close the file when you are done.
      UTL_FILE.fclose(outputfile);
    
    END LOOP;
      -- go home early today ...
    END;
    
    0 讨论(0)
  • 2020-12-19 14:41

    I found a better solution without the need to declare/begin/end code blocks or query statements.

    A sample spool filename with the date and time can be achieved by:

    
    sql> column dt new_value _dt 
    sql> select to_char(sysdate,'ddMONyyyy_hh24mi') dt from dual; 
    sql> spool &_dt
    
    My file name:

    27JUN2011_1727.lst

    You can even specify the file extension if you need to (eg .txt). Just create another variable.

    source: http://oracle.ittoolbox.com/groups/technical-functional/oracle-apps-l/variable-file-name-with-spool-1508529

    0 讨论(0)
提交回复
热议问题