07 October 2024

truncate tables with a suffix

While working with D365 F&O I often have to delete test data from several tables with a prefix or suffix. The following SQL script helps a lot to generate the truncate commands for these tables. For example to empty all staging tables from a BYOD.


DECLARE @suffix NVARCHAR(50) = 'staging'; 


SELECT TABLE_SCHEMA, TABLE_NAME, 'truncate table ' + TABLE_NAME as SQLCommand

FROM INFORMATION_SCHEMA.TABLES

WHERE TABLE_NAME LIKE '%' + @suffix;

No comments: