如何删除包含特定字符串的所有表?

How to drop all tables that contain a certain string?

我需要删除以 "my_database.test_table5 . . . "

开头的表

我该怎么做?

DROP TABLE * 
WHERE tablename IN 
(
SEL tablename 
FROM dbc.tables 
WHERE tablename like '%test_table5%' 
AND databasename = 'my_database' 
) 

您需要编写并执行一些动态的 SQL 来实现它,因为您不能像替换字段值那样动态替换 SQL 语句中的对象。尽管很容易将两者混淆。

在你的情况下,一个非常准系统的存储过程可以做你想做的事情看起来像:

CREATE PROCEDURE your_db.drop_tables(IN tableName VARCHAR(30), IN dbName VARCHAR(30))
BEGIN

    --Variable for storing the matching table name inside the cursor loop
    DECLARE matchingTable VARCHAR(30);

    --Variable for storing the dynamic sql we will execute
    DECLARE strSQL VARCHAR(500);

    --Build the cursor for matching table names in the database.
    DECLARE table_cursor CURSOR FOR 
        SELECT tablename 
        FROM dbc.tables 
        WHERE tablename LIKE '%' || :tableName || '%' 
            AND databasename = :dbName;

    --Open the cursor and loop through results
    OPEN table_cursor;
    label_cursor_loop:
    LOOP

        --Catch any errors and get out if there is trouble.
        FETCH table_cursor INTO matchingTable;
        IF (SQLSTATE = '02000') THEN
          LEAVE label_cursor_loop;
        END IF;

        --Set up the drop table statement
        SET strSQL = 'DROP TABLE "' || dbName || '"."' || tableName || '";';

        --Execute the drop table statement
        CALL DBC.SysExecSQL( sqlStatement );

    END LOOP label_cursor_loop;
    CLOSE table_cursor; 

END;

然后您只需调用它并传递您的数据库和搜索词以搜索 table 个名称,例如:

CALL your_db.drop_tables('test_table5', 'my_database')

您也可以选择将该搜索词和数据库硬编码到 SQL 语句中,但这样更有趣,对吧?

最后,这将不分青红皂白地删除任何与您的搜索词匹配的 table,因此请谨慎行事。保持安全严密,仅在需要核弹选项时使用。另外,我只是在没有执行 DROP TABLE sql 的情况下对此进行了轻微测试。所以请注意买者和所有这些。

如果这是一次性任务并且您根本不想手动删除 20 个表,让数据库为您写入 SQL。

 Select 'drop table' ¦¦ databasename ¦¦ '.'  ¦¦ tablename ¦¦ ';' 
   From Dbc.TablesV
  Where databasename = 'mydatabase' 
    And tablename like 'test_table5%' ;

然后复制这个查询的结果并执行。

您会发现这种 'let the database write your SQL' 模式会大大简化您的任务。

但是,如果这是一项重复任务,或者要分批完成,请毫无疑问地使用程序解决方案。

根据之前的回答和评论,加上一些实际使它工作的修复,只是认为我会 post 一个工作过程:

REPLACE PROCEDURE <your_db>.drop_tables(IN tableName VARCHAR(128), IN dbName VARCHAR(128))
BEGIN

--Variable for storing the matching table name inside the cursor loop
DECLARE matchingTable VARCHAR(128);

--Variable for storing the dynamic sql we will execute
DECLARE strSQL VARCHAR(5000);

--Build the cursor for matching table names in the database.
DECLARE table_cursor CURSOR FOR 
    SELECT tablename 
    FROM dbc.TablesV 
    WHERE tablename LIKE :tableName
        AND databasename = :dbName;

--Open the cursor and loop through results
OPEN table_cursor;
label_cursor_loop:
LOOP

    --Catch any errors and get out if there is trouble.
    FETCH table_cursor INTO matchingTable;
    IF (SQLSTATE = '02000') THEN
      LEAVE label_cursor_loop;
    END IF;

    --Set up the drop table statement
    SET strSQL = 'DROP TABLE "' || dbName || '"."' || matchingTable || '";';

    --Execute the drop table statement
    CALL DBC.SysExecSQL( strSQL );

END LOOP label_cursor_loop;
CLOSE table_cursor; 
END;