/*--------------------------------
功能說明: 批量DropTable
使用說明: 使用時一定要小心,因為刪選表的where條件是like所有必須保證where
後的like確定與你要刪除表名相匹配
---------------------------------*/
--------參數定義-------------------
DECLARE @tableName AS Nvarchar(50) --查詢表名條件(小心!,
確保like條件是你要Drop的表.TableName盡量精確)
SET @tableName='test'
--------------------------------------
--SELECT name FROM sys.tables WHERE name LIKE '%'+@tableName+'%' --查詢出要刪除表的名稱
IF @tableName='' SET @tableName='tableName'--初始化TableName為tableName,防止@tableName為空
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)