2016-06-10 15 views
0

ich unter delete_import.bat-Datei, die fein läuft, und es hat folgenden Inhalt:Wie Protokoll von SQL-Skript zu erzeugen, aus Batch-Datei ausgeführt wird

sqlplus @C:\Exp_Imp_Util\delete_tmc.sql 

Die deletetmc.sql Dateiinhalte alle Datenbankobjekte, die ich will, löschen. Aber jetzt, wenn ich die Batchdatei ausführe, sollte es ein Protokoll aller Löschanweisungen erstellen und es sollte auch in die Protokolldatei geschrieben werden, wenn beim Löschen der SQL-Anweisungen ein Oracle-Fehler auftrat.

CONNECT TMC/TMC; 
spool off 
declare 
stringa varchar2(100); 

cursor cur is 
select * 
from user_objects; 

begin 
for c in cur loop 
begin 
stringa := ''; 

if c.object_type = 'VIEW' then 

stringa := 'drop view ' || c.object_name; 
EXECUTE immediate stringa; 

elsif c.object_type = 'TABLE' then 

stringa := 'drop table ' || c.object_name || ' cascade constraints'; 
EXECUTE immediate stringa; 

elsif c.object_type = 'SEQUENCE' then 

stringa := 'drop sequence ' || c.object_name; 
EXECUTE immediate stringa; 
elsif c.object_type = 'PACKAGE' then 

stringa := 'drop package ' || c.object_name; 
EXECUTE immediate stringa;  

elsif c.object_type = 'TRIGGER' then 

stringa := 'drop trigger ' || c.object_name; 
EXECUTE immediate stringa;  

elsif c.object_type = 'PROCEDURE' then 

stringa := 'drop procedure ' || c.object_name; 
EXECUTE immediate stringa; 

elsif c.object_type = 'FUNCTION' then 

stringa := 'drop function ' || c.object_name; 
EXECUTE immediate stringa;  
elsif c.object_type = 'SYNONYM' then 

stringa := 'drop synonym ' || c.object_name; 
EXECUTE immediate stringa; 
elsif c.object_type = 'INDEX' then 

stringa := 'drop index ' || c.object_name; 
EXECUTE immediate stringa; 
elsif c.object_type = 'PACKAGE BODY' then 

stringa := 'drop PACKAGE BODY ' || c.object_name; 
EXECUTE immediate stringa;  

end if; 

    exception 
when others then 
null; 
end; 
end loop; 
-- PURGE recyclebin 

end; 
/
EXIT; 

Antwort

3

Sie können SPOOL auf eine Datei zu schreiben und dann verwenden DBMS_OUTPUT:

script.sql:

spool spool.txt 
set serveroutput on 
declare 
    vSQL varchar2(1000); 
begin 
    vSQL := 'create table tab1 (a number)'; 
    begin 
     execute immediate vSQL; 
     dbms_output.put_line('OK - ' || vSQL); 
    exception 
    when others then 
     dbms_output.put_line('KO - ' || vSQL || ' - ' || sqlerrm);   
    end; 

    vSQL := 'drop table tab1'; 
    begin 
     execute immediate vSQL; 
     dbms_output.put_line('OK - ' || vSQL); 
    exception 
    when others then 
     dbms_output.put_line('KO - ' || vSQL || ' - ' || sqlerrm);   
    end; 

    vSQL := 'drop table tab1'; 
    begin 
     execute immediate vSQL; 
     dbms_output.put_line('OK - ' || vSQL); 
    exception 
    when others then 
     dbms_output.put_line('KO - ' || vSQL || ' - ' || sqlerrm);   
    end; 
end; 
/ 
spool off 

Nachdem das Skript ausgeführt wird, die Datei spool.txt wird sein:

OK - create table tab1 (a number)            
OK - drop table tab1                
KO - drop table tab1 - ORA-00942: table or view does not exist     

PL/SQL procedure successfully completed. 
+0

Ich habe es versucht, aber in der Protokolldatei zeigt es als Verbunden. Zum Testen habe ich eine Tabelle erstellt und versucht, diese Batch-Datei zu löschen. Die Tabelle wurde gelöscht, aber in der Protokolldatei wurde sie als Verbunden geschrieben. Keine Information darüber, welche Tabelle gelöscht wurde. – Andrew

+0

Vielleicht hast du die CONN an der falschen Stelle; versuche es vor einer anderen Aussage zu verschieben – Aleksej