Truncate multiple tables in one MySQL statement

╄→尐↘猪︶ㄣ 提交于 2019-12-18 02:15:11

问题


Is there a possibility to truncate with one SQL statement, multiple tables?

Like this:

 truncate table #OBJ_AvailabilityTraining, #OBJ_AvailabilityHoliday, #Dates_temp;

Regards


回答1:


No, you can only truncate a single table with TRUNCATE command. To truncate multiple tables you can use T-SQL and iterate through table names to truncate each at a time.

DECLARE @delimiter CHAR(1),
        @tableList VARCHAR(MAX),
        @tableName VARCHAR(20),
        @currLen INT

SET @delimiter = ','

SET @tableList = 'table1,table2,table3'

WHILE LEN(@tableList) > 0
BEGIN
    SELECT @currLen = 
    (
        CASE charindex( @delimiter, @tableList ) 
            WHEN 0 THEN len( @tableList  ) 
            ELSE ( charindex( @delimiter, @tableList  ) -1 )
        END
    ) 

    SELECT @tableName = SUBSTRING (@tableList,1,@currLen )

    TRUNCATE TABLE @tableName

    SELECT tableList = 
    (
        CASE ( len( @tableList ) - @currLen  ) 
            WHEN 0 THEN '' 
            ELSE right( @tableList, len( @tableList ) - @currLen  - 1 ) 
        END
    ) 
END

You can have all your table names comma separated in @tableList variable and yes you can truncate multiple tables from different schemas if they are prefixed.




回答2:


You can use the sp_MSforeachtable stored procedure like so:

USE MyDatabase
EXEC sp_MSforeachtable 'TRUNCATE TABLE ?'

Or you can create SQL Statement

SELECT concat('TRUNCATE TABLE ', TABLE_NAME, ';')
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE 'TableName%'

and run this above SQL statement




回答3:


No. But there's an alternative:

SELECT 'TRUNCATE TABLE '+TABLE_NAME+ ';'
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE 'your-table-name%'

Example :

SELECT 'TRUNCATE TABLE '+TABLE_NAME+ ';'
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE 'TBL_ORDERS_20%'

Now you have below Results for above Select Query

TRUNCATE TABLE TBL_ORDERS_2001
TRUNCATE TABLE TBL_ORDERS_2002
TRUNCATE TABLE TBL_ORDERS_2003
TRUNCATE TABLE TBL_ORDERS_2004

or you can use something like

select 'Truncate table ' + TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_NAME in ('Table1', 'Table2')

Link 1

Link 2

Update:

Looking at table in your Example Query in your Question

truncate table #OBJ_AvailabilityTraining, #OBJ_AvailabilityHoliday, #Dates_temp

I think you want to Truncate all temporary tables.

You can do so with a simple Query like this

select 'Truncate table ' + TABLE_NAME from tempdb.INFORMATION_SCHEMA.TABLES



回答4:


Bonus example, truncate all tables in one database starting with a string...

SELECT CONCAT('TRUNCATE TABLE ', TABLE_NAME, ';')
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'my_database_name'
  AND TABLE_NAME LIKE 'cache_%';



回答5:


Just easily enter this MySQL statement:

TRUNCATE TABLE yourtablename1;
TRUNCATE TABLE yourtablename2;
TRUNCATE TABLE yourtablename3;

... and so on.

Change the yourtablename with yours.

Don't forget the semicolon ;




回答6:


I have had to change the code sent by IndoKnight because it throws me an error in the truncate statement so I change the code by this, it is quite similar but the truncate part:

DECLARE @delimiter CHAR(1),
        @tableList VARCHAR(MAX),
        @tableName VARCHAR(100),
        @currLen INT,
        @truncateStatement VARCHAR(200)

SET @delimiter = ','

SELECT @tableList = COALESCE(@tableList + ', ','') + CAST (TABLE_NAME AS varchar(100))
    FROM INFORMATION_SCHEMA.TABLES
    WHERE TABLE_SCHEMA = 'xxx'

    WHILE LEN(@tableList) > 0
    BEGIN
        SELECT @currLen = 
        (
            CASE charindex( @delimiter, @tableList ) 
                WHEN 0 THEN len( @tableList  ) 
                ELSE ( charindex( @delimiter, @tableList  ) -1 )
            END
        ) 

        SELECT @tableName = TRIM(SUBSTRING (@tableList,1,@currLen ))

        SET @truncateStatement = 'TRUNCATE TABLE ' + QUOTENAME('xxx') + '.' + QUOTENAME(@tableName)
        EXEC (@truncateStatement)

        SELECT @tableList = 
        (
            CASE ( len( @tableList ) - @currLen  ) 
                WHEN 0 THEN '' 
                ELSE right( @tableList, len( @tableList ) - @currLen  - 1 ) 
            END
        ) 
    END

The trick is using QUOTENAME to put the square brackets to the table name and schema. Maybe IndoKnight solution does not work anymore in more recent version of the database. I hope it helps.



来源:https://stackoverflow.com/questions/15926826/truncate-multiple-tables-in-one-mysql-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!