MSSQL: truncate all tables from schema with ‘test’ in the table name


DECLARE @sql NVARCHAR(MAX) = ''

SELECT @sql = @sql + 'TRUNCATE TABLE  [' + TABLE_SCHEMA + '].[' + TABLE_NAME + '] ' + CHAR(10)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'MYSCHEMA'  -- Change this to your schema
AND TABLE_NAME LIKE '%test%'  -- Find tables containing 'test' in the name

PRINT @sql  -- Check the generated SQL (optional)
EXEC sp_executesql @sql  -- Execute the truncation

Comments are closed.