其实主要是把网上的内容整合了一下。网上的关于索引与触发器的只有针对个别表的,我扩展到了数据库。

DROP Constraints
--- Make sure we are on the correct db

USE [TestDB1]
GO
--- Do not show record counts

SET NOCOUNT ON

--- Declare variables to be use in script

DECLARE @ObjectID INT
, @ObjectName NVARCHAR(500)
, @SQL NVARCHAR(2000)
, @Count INT
, @ObjectType nVARCHAR(1000)

--- Create temp table that is going to be used throughout the script

CREATE TABLE #ConstraintTemp (ObjectID INT IDENTITY(1,1) NOT NULL, ObjectName NVARCHAR(250), ObjectType NVARCHAR(100))
SET @Count = 0

--- First let's drop all the constraints on the tables
------ CAUTION: Running this part removes all constraints from the database -------

INSERT INTO #ConstraintTemp (ObjectName, ObjectType)
SELECT Table_Schema + '.' + Table_Name, Constraint_Name
FROM INFORMATION_SCHEMA.Table_CONSTRAINTS
ORDER BY constraint_type , Table_Name

SELECT @ObjectID = MIN(ObjectID) FROM #ConstraintTemp

WHILE @ObjectID IS NOT NULL
BEGIN

SELECT @ObjectName = ObjectName
, @ObjectType = ObjectType
FROM #ConstraintTemp WHERE ObjectID = @ObjectID

SET @SQL = 'ALTER TABLE ' + @ObjectName + ' DROP CONSTRAINT [' + @ObjectType + ']'

EXECUTE SP_EXECUTESQL @SQL


SELECT @ObjectID = MIN(ObjectID) FROM #ConstraintTemp WHERE ObjectID > @ObjectID
SET @ObjectName = NULL
SET @SQL = NULL
SET @COUNT = @Count + 1

--DROP table #ConstraintTemp
END
PRINT CAST(@Count AS NVARCHAR(10)) + ' Constraint(s) deleted'


Code
use TestDB1
GO

DECLARE @SqlCmd NVARCHAR(4000)
declare @Trig sysname
declare @owner sysname
declare @uid int

DECLARE TGCursor CURSOR FOR
SELECT name, uid FROM sysobjects WHERE type = 'TR'
OPEN TGCursor
FETCH next FROM TGCursor INTO @Trig, @uid
WHILE @@FETCH_STATUS = 0
BEGIN

set @SQLCmd = 'drop trigger [' + user_name(@uid) + '].[' + @Trig + ']'
exec sp_executesql @SQLCmd


print @SQLCmd


FETCH next FROM TGCursor INTO @Trig, @uid
END
CLOSE TGCursor
DEALLOCATE TGCursor

GO


Code
use TestDB2

declare @tablename nvarchar(500)
declare @tid int
set @tid=1

select identity(int,1,1) as tid,name into #temp1 from sys.objects where type='U'

while(@tid<(select MAX(tid) from #temp1)+1)
begin
select @tablename=name from #temp1 where tid=@tid

declare @name nvarchar(50)
,@errorsave int

if (rtrim(object_id(@tablename)) = '')
RAISERROR ('A non-zero length table name parameter is expected', 16, 1)

BEGIN TRAN

if exists (select name from sysindexes
where id = object_id(@tablename) and indid > 0 and indid < 255 and (status & 64)=0)
begin
declare ind_cursor cursor for
select name from sysindexes
where id = object_id(@tablename) and indid > 0 and indid < 255 and (status & 64)=0

open ind_cursor
fetch next from ind_cursor into @name
while (@@fetch_status = 0)
begin
print 'delete index '+@name
exec ('drop index ' + @tablename + '.' + @name)
set @errorsave = @@error
fetch next from ind_cursor into @name
end
close ind_cursor
deallocate ind_cursor
end

if (@errorsave = 0)
COMMIT TRAN
else
ROLLBACK TRAN

set @tid=@tid+1
end
drop table #temp1

DROP Statistics
use TestDB1

DECLARE @ObjectName sysname
DECLARE @StatsName sysname
DECLARE StatsCursor CURSOR FAST_FORWARD
FOR
SELECT OBJECT_NAME(object_id) as 'ObjectName', [name] as 'StatsName' FROM sys.stats
WHERE (INDEXPROPERTY(object_id, [name], 'IsAutoStatistics') = 1 OR INDEXPROPERTY(object_id, [name], 'IsStatistics') = 1)
AND OBJECTPROPERTY(object_id, 'IsMSShipped') = 0
OPEN StatsCursor
FETCH NEXT FROM StatsCursor
INTO @ObjectName, @StatsName
WHILE @@FETCH_STATUS = 0
BEGIN
print @ObjectName + '.' + @StatsName
--EXEC ('DROP STATISTICS ' + @ObjectName + '.' + @StatsName)
FETCH NEXT FROM StatsCursor
INTO @ObjectName, @StatsName
END
CLOSE StatsCursor
DEALLOCATE StatsCursor