/*-------------------------------- Function Description: Batch DropTable Usage instructions: Be careful when using it, because the where condition for deleting the selected table is like. All where conditions must be guaranteed. The like parameter matches the name of the table to be deleted. ---------------------------------*/ -------- Parameter definition ------------------- DECLARE @ tableName AS Nvarchar (50) -- query the table name conditions (Be careful !, Make sure that the like condition is the table you want to Drop. TableName is as accurate as possible) SET @ tableName = 'test' -------------------------------------- -- SELECT name FROM sys. tables WHERE name LIKE '%' + @ tableName + '%' -- query the name of the table to be deleted. IF @ tableName = ''set @ tableName = 'tablename' -- initialize tableName to TableName to prevent @ tableName from being empty DECLARE @ tableNames AS Nvarchar (3000) DECLARE @ SQL AS Nvarchar (3000) SET @ tableNames = (SELECT ',' + name FROM sys. tables WHERE name LIKE '%' + @ tableName + '%' for xml path ('')) SET @ tableNames = Stuff (@ tableNames, 1, 1 ,'') SET @ SQL = 'drop table' + @ tableNames EXEC (@ SQL) |