【问题标题】:DB2 Drop multiple tables - auto-generating scriptDB2 删除多个表 - 自动生成脚本
【发布时间】:2016-08-21 12:05:11
【问题描述】:

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

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

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

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

db2 -tvf filename>log

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

实际上,我不知道该怎么做……请给个建议。

非常感谢!

【问题讨论】:

  • 不确定您的问题是什么。您是否尝试运行这两个命令?结果符合预期吗?

标签: sql db2


【解决方案1】:

这个脚本应该为你提供一个前进的基础:

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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 2013-02-03
    • 2020-03-12
    • 1970-01-01
    • 2012-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多