【推薦】(SqlServer)不公開存儲(chǔ)過程sp_Msforeachtable與sp_Msforeachdb詳解
【推薦】(SqlServer)不公開存儲(chǔ)過程sp_Msforeachtable與sp_Msforeachdb詳解
——通過知識(shí)共享樹立個(gè)人品牌。
一.簡(jiǎn)要介紹:
系統(tǒng)存儲(chǔ)過程sp_MSforeachtable和sp_MSforeachdb,是微軟提供的兩個(gè)不公開的存儲(chǔ)過程。從mssql6.5開始,存放在SQL Server的MASTER數(shù)據(jù)庫中。可以用來對(duì)某個(gè)數(shù)據(jù)庫的所有表或某個(gè)SQL服務(wù)器上的所有數(shù)據(jù)庫進(jìn)行管理,下面將對(duì)此進(jìn)行詳細(xì)介紹。
作為數(shù)據(jù)庫管理者或開發(fā)者等經(jīng)常會(huì)檢查整個(gè)數(shù)據(jù)庫或用戶表。
如:檢查整個(gè)數(shù)據(jù)庫的容量,看指定數(shù)據(jù)庫所有用戶表的容量,所有表的記錄數(shù)等等,我們一般處理這樣的問題都是通過游標(biāo)來達(dá)到要求。
如果我們用sp_MSforeachtable就可以非常方便的達(dá)到相同的目的,
如:sql查詢所有用戶表的列表,詳細(xì)信息,如:記錄數(shù),表占用大小等
二.各參數(shù)說明:
@replacechar nchar(1) = N'?', --指定的占位符號(hào)
@command2 nvarchar(2000)= null, --第二條運(yùn)行的SQL指令
@command3 nvarchar(2000) = null, --第三條運(yùn)行的SQL指令
@whereand nvarchar(2000) = null, --可選條件來選擇表
@precommand nvarchar(2000)= null, --執(zhí)行指令前的操作(類似控件的觸發(fā)前的操作)
@postcommand nvarchar(2000)= null --執(zhí)行指令后的操作(類似控件的觸發(fā)后的操作)
以后為sp_MSforeachtable的參數(shù),sp_MSforeachdb不包括參數(shù)@whereand
我們?cè)?/span>master數(shù)據(jù)庫里執(zhí)行下面的語句可以看到兩個(gè)proc詳細(xì)的代碼
exec sp_helptext sp_MSforeachtable
exec sp_helptext sp_Msforeachdb
三、使用舉例:
--統(tǒng)計(jì)數(shù)據(jù)庫里每個(gè)表的詳細(xì)情況:
--獲得每個(gè)表的記錄數(shù)和容量:
EXEC sp_MSforeachtable @command1="print '?'",
@command2="sp_spaceused '?'",
@command3= "SELECT count(*) FROM ? "
--獲得所有的數(shù)據(jù)庫的存儲(chǔ)空間:
EXEC sp_MSforeachdb @command1="print '?'",
@command2="sp_spaceused "
--檢查所有的數(shù)據(jù)庫
EXEC sp_MSforeachdb @command1="print '?'",
@command2="DBCC CHECKDB (?) "
--更新PUBS數(shù)據(jù)庫中已t開頭的所有表的統(tǒng)計(jì):
EXEC sp_MSforeachtable @whereand="and name like 't%'",
@replacechar='*',
@precommand="print 'Updating Statistics.....' print ''",
@command1="print '*' update statistics * ",
@postcommand= "print''print 'Complete Update Statistics!'"
--刪除當(dāng)前數(shù)據(jù)庫所有表中的數(shù)據(jù)
sp_MSforeachtable @command1='Delete from ?'
sp_MSforeachtable @command1 = "TRUNCATE TABLE ?"
--查詢數(shù)據(jù)庫所有表的記錄總數(shù)
CREATE TABLE #temp (TableName VARCHAR (255), RowCnt INT)
EXEC sp_MSforeachtable 'INSERT INTO #temp SELECT ''?'', COUNT(*) FROM ?'
SELECT TableName, RowCnt FROM #temp ORDER BY TableName
DROP TABLE #temp
--檢查數(shù)據(jù)庫里每個(gè)表或索引視圖的數(shù)據(jù)、索引及text、ntext 和image 頁的完整性
--下列語句需在單用戶模式下執(zhí)行(sp_dboption 'db_name', 'single user', 'true')
--,將true改成false就又變成多用戶了
exec sp_msforeachtable "dbcc checktable('?',repair_rebuild)"
4.參數(shù)@whereand的用法:
@whereand參數(shù)在存儲(chǔ)過程中起到指令條件限制的作用,具體的寫法如下:
@whereend,可以這么寫
又如:
我想更新Table1/Table2/Table3中NOTE列為NULL的值
,@whereand=' AND o.name in (''Table1'',''Table2'',''Table3'')'
5. "?"特別說明:
"?"在存儲(chǔ)過程的特殊用法,造就了這兩個(gè)功能強(qiáng)大的存儲(chǔ)過程.
"?"的作用,相當(dāng)于DOS命令中和我們?cè)赪INDOWS下搜索文件時(shí)的通配符的作用。
6.小結(jié)
有上面的分析,我們可以建立自己的sp_MSforeachObject:(注:下面的內(nèi)容來源于網(wǎng)上。)
USE MASTER
CREATE proc sp_MSforeachObject
@objectType int=1,
@command1 nvarchar(2000),
@replacechar nchar(1) = N'?',
@command2 nvarchar(2000) = null,
@command3 nvarchar(2000) = null,
@whereand nvarchar(2000) = null,
@precommand nvarchar(2000) = null,
@postcommand nvarchar(2000) = null
as
/* This proc returns one or more rows for each table (optionally, matching @where), with each table defaulting to its
own result set */
/* @precommand and @postcommand may be used to force a single result set via a temp table. */
/* Preprocessor won't replace within quotes so have to use str(). */
declare @mscat nvarchar(12)
select @mscat = ltrim(str(convert(int, 0x0002)))
if (@precommand is not null)
exec(@precommand)
/* Defined @isobject for save object type */
Declare @isobject varchar(256)
select @isobject= case @objectType when 1 then 'IsUserTable'
when 2 then 'IsView'
when 3 then 'IsTrigger'
when 4 then 'IsProcedure'
when 5 then 'IsDefault'
when 6 then 'IsForeignKey'
when 7 then 'IsScalarFunction'
when 8 then 'IsInlineFunction'
when 9 then 'IsPrimaryKey'
when 10 then 'IsExtendedProc'
when 11 then 'IsReplProc'
when 12 then 'IsRule'
end
/* Create the select */
/* Use @isobject variable isstead of IsUserTable string */
EXEC(N'declare hCForEach cursor global for select ''['' + REPLACE(user_name(uid), N'']'', N'']]'') + '']'' + ''.'' + ''['' +
REPLACE(object_name(id), N'']'', N'']]'') + '']'' from dbo.sysobjects o '
+ N' where OBJECTPROPERTY(o.id, N'''+@isobject+''') = 1 '+N' and o.category & ' + @mscat + N' = 0 '
+ @whereand)
declare @retval int
select @retval = @@error
if (@retval = 0)
exec @retval = sp_MSforeach_worker @command1, @replacechar, @command2, @command3
if (@retval = 0 and @postcommand is not null)
exec(@postcommand)
return @retval
GO
我們來測(cè)試一下:
EXEc sp_MSforeachObject @command1="sp_helptext '?' ",@objectType=4
--獲得所有的視圖的腳本:
EXEc sp_MSforeachObject @command1="sp_helptext '?' ",@objectType=2
--比如在開發(fā)過程中,沒一個(gè)用戶都是自己的OBJECT OWNER,所以在真實(shí)的數(shù)據(jù)庫時(shí)都要改為DBO:
EXEc sp_MSforeachObject @command1="sp_changeobjectowner '?', 'dbo'",@objectType=1
EXEc sp_MSforeachObject @command1="sp_changeobjectowner '?', 'dbo'",@objectType=2
EXEc sp_MSforeachObject @command1="sp_changeobjectowner '?', 'dbo'",@objectType=3
EXEc sp_MSforeachObject @command1="sp_changeobjectowner '?', 'dbo'",@objectType=4
作者:
RDIF
出處:
http://www.rzrgm.cn/huyong/
Email:
406590790@qq.com
QQ:
406590790
微信:
13005007127(同手機(jī)號(hào))
框架官網(wǎng):
http://www.guosisoft.com/
http://www.rdiframework.net/
框架其他博客:
http://blog.csdn.net/chinahuyong
http://www.rzrgm.cn/huyong
國思RDIF開發(fā)框架
,
給用戶和開發(fā)者最佳的.Net框架平臺(tái)方案,為企業(yè)快速構(gòu)建跨平臺(tái)、企業(yè)級(jí)的應(yīng)用提供強(qiáng)大支持。
關(guān)于作者:系統(tǒng)架構(gòu)師、信息系統(tǒng)項(xiàng)目管理師、DBA。專注于微軟平臺(tái)項(xiàng)目架構(gòu)、管理和企業(yè)解決方案,多年項(xiàng)目開發(fā)與管理經(jīng)驗(yàn),曾多次組織并開發(fā)多個(gè)大型項(xiàng)目,在面向?qū)ο蟆⒚嫦蚍?wù)以及數(shù)據(jù)庫領(lǐng)域有一定的造詣。現(xiàn)主要從事基于
RDIF
框架的技術(shù)開發(fā)、咨詢工作,主要服務(wù)于金融、醫(yī)療衛(wèi)生、鐵路、電信、物流、物聯(lián)網(wǎng)、制造、零售等行業(yè)。
如有問題或建議,請(qǐng)多多賜教!
本文版權(quán)歸作者和CNBLOGS博客共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,如有問題,可以通過微信、郵箱、QQ等聯(lián)系我,非常感謝。

系統(tǒng)存儲(chǔ)過程sp_MSforeachtable和sp_MSforeachdb,是微軟提供的兩個(gè)不公開的存儲(chǔ)過程。從mssql6.5開始,存放在SQL Server的MASTER數(shù)據(jù)庫中。可以用來對(duì)某個(gè)數(shù)據(jù)庫的所有表或某個(gè)SQL服務(wù)器上的所有數(shù)據(jù)庫進(jìn)行管理,下面將對(duì)此進(jìn)行詳細(xì)介紹。
作為數(shù)據(jù)庫管理者或開發(fā)者等經(jīng)常會(huì)檢查整個(gè)數(shù)據(jù)庫或用戶表。
如:檢查整個(gè)數(shù)據(jù)庫的容量,看指定數(shù)據(jù)庫所有用戶表的容量,所有表的記錄數(shù)等等,我們一般處理這樣的問題都是通過游標(biāo)來達(dá)到要求。
如果我們用sp_MSforeachtable就可以非常方便的達(dá)到相同的目的,
如:sql查詢所有用戶表的列表,詳細(xì)信息,如:記錄數(shù),表占用大小等
EXEC sp_MSforeachtable "EXECUTE sp_spaceused '?'"
浙公網(wǎng)安備 33010602011771號(hào)