DB2删除多个表 - 自动生成脚本

时间:2016-04-27 09:25:23

标签: sql db2

我有一些非常具体的问题。 目标是在一个超过3个月的架构中删除多个表并具有特定前缀。

困难在于我需要设计一个脚本,它会自动生成drop语句,这样我就可以将它转换为crontab进行日常执行。

很快,我需要这两个动作:

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

db2 -tvf filename>log

已打包在一个脚本中,该脚本将生成要删除的表列表,然后删除这些表。

实际上,我不知道该怎么做......请提出建议。

非常感谢!

1 个答案:

答案 0 :(得分:0)

此脚本应为您提供前进的基础:

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 

<强>结果:

/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.