Article View: cn.bbs.comp.database
Article #86ȥ��ijSQL Server��ݿ�������Լ��,����,������,ͳ��
From: Galaxy��VOLUNTEERS��Database
Date: Thu, 20 Aug 2009 07:46
Date: Thu, 20 Aug 2009 07:46
183 lines
5105 bytes
5105 bytes
��ʵ��Ҫ�ǰ����ϵ����������һ�¡����ϵĹ��������봥������ֻ����Ը����ģ�
����չ������ݿ⡣
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
--
Welcome to Database Board
��ӭ������ݿ��
http://bbs.sjtu.edu.cn/bbsdoc?board=database
[m[1;32m�� ��Դ:����ˮ˼Դ bbs.sjtu.edu.cn��[FROM: 207.46.55.29][m
Technical Details
Message-ID:
<4hTU5c$b0W@bbs.sjtu.edu.cn>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news.glorb.com!newsgate.cuhk.edu.hk!news.cn99.com!news.shec6.edu.cn!SJTUShow Full Headers
Complete Article Headers
X-Google-Thread: 4df683fd51,514fd69d5b5c1324,start X-Google-Attributes: gid4df683fd51,public,usenet X-Google-NewGroupId: yes X-Google-Language: Chinese,GB Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news.glorb.com!newsgate.cuhk.edu.hk!news.cn99.com!news.shec6.edu.cn!SJTU From: Dem.bbs@bbs.sjtu.edu.cn (Galaxy��VOLUNTEERS��Database) Newsgroups: cn.bbs.comp.database Subject: ȥ��ijSQL Server��ݿ�������Լ��,����,������,ͳ�� Date: 20 Aug 2009 07:46:54 GMT Organization: ��ˮ˼Դ Message-ID: <4hTU5c$b0W@bbs.sjtu.edu.cn> Mime-Version: 1.0 Content-Type: text/plain; charset="gb2312" Content-Transfer-Encoding: 8bit X-Filename: database/M.1250754326.A