31

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

Like this:

 truncate table #OBJ_AvailabilityTraining, #OBJ_AvailabilityHoliday, #Dates_temp;

Regards

Ravindra S
  • 6,302
  • 12
  • 70
  • 108
user2206834
  • 379
  • 1
  • 5
  • 13

6 Answers6

16

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.

sfarbota
  • 2,619
  • 1
  • 22
  • 30
IndoKnight
  • 1,846
  • 1
  • 21
  • 29
14

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

Pankaj Agarwal
  • 11,191
  • 12
  • 43
  • 59
  • perfect. I used this in SSIS using exec sql to get the list and a foreach loop to iterate through each one. I guess you could do the same in SSMS with a cursor. – KeithL Nov 07 '17 at 15:07
  • 3
    `sp_MSforeachtable` is MSSQL, this question is MySQL. – chx Sep 15 '18 at 05:00
  • 1
    When I use your below query, It is not truncating the tables but just displaying the name of the tables in the output like `Truncate ABC Truncate CDE` – MVC Jun 30 '21 at 23:44
  • This statement selects all tables in all databases. How about one database only? – Petra Sep 22 '21 at 07:57
10

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
Prahalad Gaggar
  • 11,389
  • 16
  • 53
  • 71
  • 1
    personally i had to use `SELECT concat('Truncate table ', TABLE_NAME)` to get proper output. But thanks for the general idea! – Cpt. Senkfuss Jul 12 '18 at 10:15
7

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 ;

3

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_%';
doublejosh
  • 5,548
  • 4
  • 39
  • 45
1

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.

Xavi Duro
  • 26
  • 3