UTL_FILE saving in local machine

后端 未结 2 1559
臣服心动
臣服心动 2021-01-28 14:09

Hello, I wanted to save all the functions,procedures,packages in my local machine. I tried with SPOOL but with spool im not sure to get the filename and save it. So i tried

相关标签:
2条回答
  • 2021-01-28 14:25

    You can use dbms_output.put_line in your PLSQL together with SPOOL to get the result. For example like the below.This prints ename, sal in the file_name.txt file.

        set serveroutput on  
        Spool e:\file_name.txt  
        Begin  
          for c in (Select ename, sal from emp)  
          loop  
            dbms_output.put_line(c.ename || ',' || c.sal);  
          end loop;  
        End;  
        /  
        spool off
    

    for your example, you can use the below

        set serveroutput on  
        Spool C:\Workspace\BE\DB\Funktionen\Function1.sql
        BEGIN 
        FOR a IN (SELECT distinct name,type 
        FROM all_source 
        WHERE type IN 
        ('FUNCTION')
        AND OWNER='HR')
        LOOP
        FOR b IN (SELECT text FROM all_source WHERE TYPE IN('FUNCTION')AND OWNER = 'HR' AND 
        NAME = (a.name))
        LOOP
        dbms_output.enable;
        dbms_output.put_line(b.text);
    
        END LOOP;
        END LOOP;
        EXCEPTION
        WHEN others THEN
        dbms_output.put_line( 'ERROR: Invalid PATH FOR file.'||sqlerrm);
        END;
        /  
        spool off
    

    You can generate one master file which has plsql for all the functions individually which you can run for all the functions

            set serveroutput on
        spool C:\Workspace\BE\DB\Funktionen\function12.sql
        begin
        FOR a IN (SELECT distinct name,type 
            FROM all_source 
            WHERE type IN 
            ('FUNCTION')
            AND OWNER='HR')
            loop
         dbms_output.enable  ; 
        dbms_output.put_line('set serveroutput on  
            Spool C:\Workspace\BE\DB\Funktionen\'||a.name||'.sql
            BEGIN 
            FOR a1 IN (SELECT distinct name,type 
            FROM all_source 
            WHERE type IN 
            (''FUNCTION'')
            AND OWNER=''HR'' and name='''||a.name||''')
            LOOP
            FOR b IN (SELECT text FROM all_source WHERE TYPE IN(''FUNCTION'')AND OWNER = ''HR'' AND 
            NAME = '''||a.name||'''))
            LOOP
            dbms_output.enable;
            dbms_output.put_line(b.text);
    
            END LOOP;
            END LOOP;
            EXCEPTION
            WHEN others THEN
            dbms_output.put_line( ''ERROR: Invalid PATH FOR file.''||sqlerrm);
            END;'
            );
            END LOOP;
            END;
            /
        spool off
    
    0 讨论(0)
  • 2021-01-28 14:34

    UTL_FILE writes to the database server. To write to your local machine, you would need to mount your directories on the db server. This is generally a bad idea!

    Use spool to write to your machine just do:

    spool c:\path\to\your\folder\file.name
    select ....
    
    0 讨论(0)
提交回复
热议问题