[sql] view plaincopy
/*
1) 明
系 存 程sp_MSforeachtable和sp_MSforeachdb,是微 提供的 不公 的存 程, ms sql 6.5 始。
存放在SQL Server的MASTER 料 中。
2) 明:
@command1 nvarchar(2000), --第一 行的SQL指令
@replacechar nchar(1) = N' ', --指定的占位元符
@command2 nvarchar(2000)= null, --第二 行的SQL指令
@command3 nvarchar(2000)= null, --第三 行的SQL指令
@whereand nvarchar(2000)= null, --可 件 表
@precommand nvarchar(2000)= null, -- 行指令前的操作( 似控制 的 前的操作)
@postcommand nvarchar(2000)= null -- 行指令後的操作( 似控制 的 後的操作)
*/
exec sp_msforeachtable "alter table nocheck CONSTRAINT all" --禁所有外
exec sp_msforeachtable "alter table DISABLE TRIGGER all" --禁所有 器
-- 除所有表的 ,有外 的除外
,@whereand=' and (objectproperty(o.id,''TableHasForeignRef'')=0 and objectproperty(o.id,''TableHasForeignKey'')=0) '
--把有外 的 除
exec sp_msforeachtable @command1='delete '
,@whereand=' and (objectproperty(o.id,''TableHasForeignRef'')=1 or objectproperty(o.id,''TableHasForeignKey'')=1) '
-- 所有表的信息
exec sp_MSforeachtable @command1="sp_spaceused ' '"
exec sp_msforeachtable "alter table check CONSTRAINT all" -- 所有外
exec sp_msforeachtable "alter table enable TRIGGER all" -- 所有 器
--所有identity表 原 1的
exec sp_MSforeachtable @command1='dbcc checkident([ ],reseed,0) ' ,@whereand= ' and (objectproperty(o.id,''TableHasIdentity'')=1) '
--重建所有索引
exec sp_MSforeachtable @command1='dBCC DBREINDEX ([ ]) '
摘自 MasterLonely的专栏