清除 数据库表 上面 清除 没有外间关系 下面 清除 有外键关系的
SET NoCount ON DECLARE @tableName varchar(512) Declare @SQL varchar(2048) SET @tableName='' WHILE EXISTS ( --Find all child tables and those which have no relations SELECT T.table_name FROM INFORMATION_SCHEMA.TABLES T LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC ON T.table_name = TC.table_name WHERE ( TC.constraint_Type = 'Foreign Key' OR TC.constraint_Type IS NULL ) AND T.table_name NOT IN ( 'dtproperties', 'sysconstraints', 'syssegments' ) AND Table_type = 'BASE TABLE' AND T.table_name > @TableName ) Begin SELECT @tableName = min(T.table_name) FROM INFORMATION_SCHEMA.TABLES T LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC ON T.table_name=TC.table_name WHERE ( TC.constraint_Type = 'Foreign Key' OR TC.constraint_Type IS NULL ) AND T.table_name NOT IN ( 'dtproperties', 'sysconstraints', 'syssegments' ) AND Table_type = 'BASE TABLE' AND T.table_name > @TableName --Truncate the table SET @SQL = 'Truncate table '+ @TableName print (@SQL) Exec(@SQL) End SET @TableName='' WHILE EXISTS ( --Find all Parent tables SELECT T.table_name FROM INFORMATION_SCHEMA.TABLES T LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC ON T.table_name = TC.table_name WHERE TC.constraint_Type = 'Primary Key' AND T.table_name <> 'dtproperties' AND Table_type='BASE TABLE' AND T.table_name > @TableName ) Begin SELECT @tableName = min(T.table_name) FROM INFORMATION_SCHEMA.TABLES T LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC ON T.table_name=TC.table_name WHERE TC.constraint_Type = 'Primary Key' AND T.table_name <> 'dtproperties' AND Table_type = 'BASE TABLE' AND T.table_name > @TableName --Delete the table SET @SQL = ' delete from '+ @TableName print (@SQL) Exec(@SQL) --Reset identity column IF EXISTS ( SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMNPROPERTY( OBJECT_ID( QUOTENAME(table_schema)+ '.' + QUOTENAME(@tableName) ), column_name,'IsIdentity' ) = 1 ) DBCC CHECKIDENT(@tableName,RESEED,0) End SET NoCount OFF SET NoCount ON DECLARE @tableName varchar(512) Declare @SQL varchar(2048) SET @tableName='' WHILE EXISTS ( --Find all child tables and those which have no relations SELECT T.table_name FROM INFORMATION_SCHEMA.TABLES T LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC ON T.table_name = TC.table_name WHERE ( TC.constraint_Type = 'Foreign Key' OR TC.constraint_Type IS NULL ) AND T.table_name NOT IN ( 'dtproperties', 'sysconstraints', 'syssegments' ) AND Table_type = 'BASE TABLE' AND T.table_name > @TableName ) Begin SELECT @tableName = min(T.table_name) FROM INFORMATION_SCHEMA.TABLES T LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC ON T.table_name=TC.table_name WHERE ( TC.constraint_Type = 'Foreign Key' OR TC.constraint_Type IS NULL ) AND T.table_name NOT IN ( 'dtproperties', 'sysconstraints', 'syssegments' ) AND Table_type = 'BASE TABLE' AND T.table_name > @TableName --Truncate the table SET @SQL = 'Truncate table '+ @TableName print (@SQL) Exec(@SQL) End SET @TableName='' WHILE EXISTS ( --Find all Parent tables SELECT T.table_name FROM INFORMATION_SCHEMA.TABLES T LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC ON T.table_name = TC.table_name WHERE TC.constraint_Type = 'Primary Key' AND T.table_name <> 'dtproperties' AND Table_type='BASE TABLE' AND T.table_name > @TableName ) Begin SELECT @tableName = min(T.table_name) FROM INFORMATION_SCHEMA.TABLES T LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC ON T.table_name=TC.table_name WHERE TC.constraint_Type = 'Primary Key' AND T.table_name <> 'dtproperties' AND Table_type = 'BASE TABLE' AND T.table_name > @TableName --Delete the table SET @SQL = ' delete from '+ @TableName print (@SQL) Exec(@SQL) --Reset identity column IF EXISTS ( SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMNPROPERTY( OBJECT_ID( QUOTENAME(table_schema)+ '.' + QUOTENAME(@tableName) ), column_name,'IsIdentity' ) = 1 ) DBCC CHECKIDENT(@tableName,RESEED,0) End SET NoCount OFF