2016-04-27 3 views
0

Ich habe ein sehr spezifisches Problem. Ziel ist es, mehrere Tabellen in einem Schema zu löschen, die älter als 3 Monate sind und ein bestimmtes Präfix haben.DB2 Mehrere Tabellen löschen - Skript automatisch generieren

Die Schwierigkeit besteht darin, dass ich ein Skript erstellen muss, das automatisch Anweisungen für den Fall generiert, damit ich es für die tägliche Ausführung in eine Crontab einordnen kann.

Kurz, ich brauche diese zwei Aktionen:

db2 "Select 'DROP TABLE ', tabname, ';' from syscat.tables where owner='DBUSER'" >> filename 

db2 -tvf filename>log 

in einem Skript verpackt worden, die die Liste der Tabellen generiert fallen gelassen werden und dann diese Tabellen löschen.

Eigentlich habe ich keine Ahnung, wie das geht ... Bitte geben Sie einen Rat.

Vielen Dank!

+0

nicht sicher, was Ihre Frage ist. Hast du versucht, diese beiden Befehle auszuführen? War das Ergebnis wie erwartet? – mustaccio

Antwort

0

sollte dieses Skript eine Grundlage für Sie vorwärts zu:

DROPFILE=/tmp/drop.$$.sql 
echo ${DROPFILE} 

db2 -o connect to pocdb 

# 
# Drop/Create tables for clean environment 
# 
for i in 0 1 2 3 4 5 
do 
    db2 "drop table stkdrp.t${i}" 
    db2 "create table stkdrp.t${i} (f1 integer)" 
done 

# 
# List tables, there should be six 
# 
db2 "select count(*) as tabcnt from syscat.tables where tabschema like 'STKDRP%' " 
db2 "select tabschema, tabname from syscat.tables where tabschema like 'STKDRP%' order by tabname" 

# 
# Create the drop command file 
# 
db2 "select 'DROP TABLE ' || trim(tabschema) || '.' || trim(tabname) || ';' from syscat.tables where tabschema like 'STKDRP%'" 2>&1 | grep DROP > ${DROPFILE} 

# 
# Drop the tables 
# 
db2 -tvf ${DROPFILE} 

# 
# List tables, there should be zero (0) 
# 
db2 "select count(*) as tabcnt from syscat.tables where tabschema like 'STKDRP%' " 
db2 "select tabschema, tabname from syscat.tables where tabschema like 'STKDRP%' order by tabname" 

# 
# Clean up the mess 
# 
rm -f ${DROPFILE} 

db2 connect reset 
db2 terminate 

Ergebnisse:

/tmp/drop.1607.sql 

    Database Connection Information 

Database server  = DB2/LINUXX8664 10.5.3 
SQL authorization ID = DB2INST1 
Local database alias = POCDB 

DB21034E The command was processed as an SQL statement because it was not a 
valid Command Line Processor command. During SQL processing it returned: 
SQL0204N "STKDRP.T0" is an undefined name. SQLSTATE=42704 
DB20000I The SQL command completed successfully. 
DB21034E The command was processed as an SQL statement because it was not a 
valid Command Line Processor command. During SQL processing it returned: 
SQL0204N "STKDRP.T1" is an undefined name. SQLSTATE=42704 
DB20000I The SQL command completed successfully. 
DB21034E The command was processed as an SQL statement because it was not a 
valid Command Line Processor command. During SQL processing it returned: 
SQL0204N "STKDRP.T2" is an undefined name. SQLSTATE=42704 
DB20000I The SQL command completed successfully. 
DB21034E The command was processed as an SQL statement because it was not a 
valid Command Line Processor command. During SQL processing it returned: 
SQL0204N "STKDRP.T3" is an undefined name. SQLSTATE=42704 
DB20000I The SQL command completed successfully. 
DB21034E The command was processed as an SQL statement because it was not a 
valid Command Line Processor command. During SQL processing it returned: 
SQL0204N "STKDRP.T4" is an undefined name. SQLSTATE=42704 
DB20000I The SQL command completed successfully. 
DB21034E The command was processed as an SQL statement because it was not a 
valid Command Line Processor command. During SQL processing it returned: 
SQL0204N "STKDRP.T5" is an undefined name. SQLSTATE=42704 
DB20000I The SQL command completed successfully. 

TABCNT 
----------- 
      6 

    1 record(s) selected. 


TABSCHEMA TABNAME                          
---------- ----------- 
STKDRP T0                           
STKDRP T1                           
STKDRP T2                           
STKDRP T3                           
STKDRP T4                           
STKDRP T5                           

    6 record(s) selected. 

DROP TABLE STKDRP.T0 
DB20000I The SQL command completed successfully. 

DROP TABLE STKDRP.T1 
DB20000I The SQL command completed successfully. 

DROP TABLE STKDRP.T2 
DB20000I The SQL command completed successfully. 

DROP TABLE STKDRP.T3 
DB20000I The SQL command completed successfully. 

DROP TABLE STKDRP.T4 
DB20000I The SQL command completed successfully. 

DROP TABLE STKDRP.T5 
DB20000I The SQL command completed successfully. 


TABCNT 
----------- 
      0 

    1 record(s) selected. 


TABUTHEMA TABNAME                          
---------- ------- 

    0 record(s) selected. 

DB20000I The SQL command completed successfully. 
DB20000I The TERMINATE command completed successfully.