日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
存儲(chǔ)過(guò)程:sp_MSforeachtable/sp_MSforeachdb

SQL Server數(shù)據(jù)庫(kù)的兩個(gè)存儲(chǔ)過(guò)程sp_MSforeachtable/sp_MSforeachdb的參數(shù)說(shuō)明及使用方法是本文我們主要要介紹的內(nèi)容,接下來(lái)就讓我們來(lái)一起了解一下這部分內(nèi)容吧。

1.簡(jiǎn)介:

作為DBA會(huì)經(jīng)常需要檢查所有的數(shù)據(jù)庫(kù)或用戶表,比如:檢查所有數(shù)據(jù)庫(kù)的容量;看看指定數(shù)據(jù)庫(kù)所有用戶表的容量,所有表的記錄數(shù)...,我們一般處理這樣的問題都是用游標(biāo)分別處理處理,比如:在數(shù)據(jù)庫(kù)檢索效率非常慢時(shí),我們想檢查數(shù)據(jù)庫(kù)所有的用戶表,我們就必須通過(guò)寫游標(biāo)來(lái)達(dá)到要求;如果我們用sp_MSforeachtable就可以非常方便的達(dá)到相同的目的:EXEC sp_MSforeachtable @command1="print '?' DBCC CHECKTABLE ('?')"
系統(tǒng)存儲(chǔ)過(guò)程sp_MSforeachtable和sp_MSforeachdb,是微軟提供的兩個(gè)不公開的存儲(chǔ)過(guò)程,從mssql6.5開始。存放在SQL Server的MASTER數(shù)據(jù)庫(kù)中??梢杂脕?lái)對(duì)某個(gè)數(shù)據(jù)庫(kù)的所有表或某個(gè)SQL服務(wù)器上的所有數(shù)據(jù)庫(kù)進(jìn)行管理,后面將對(duì)此進(jìn)行詳細(xì)介紹。

2.參數(shù)說(shuō)明:
@command1 nvarchar(2000), --第一條運(yùn)行的SQL指令
@replacechar nchar(1) = N'?',--指定的占位符號(hào)
@command2 nvarchar(2000)= null, --第二條運(yùn)行的SQL指令
@command3 nvarchar(2000)= null, --第三條運(yùn)行的SQL指令
@whereand nvarchar(2000)= null, --可選條件來(lái)選擇表
@precommand nvarchar(2000)= null, --執(zhí)行指令前的操作(類似控件的觸發(fā)前的操作)
@postcommand nvarchar(2000)= null --執(zhí)行指令后的操作(類似控件的觸發(fā)后的操作)

以后為sp_MSforeachtable的參數(shù),sp_MSforeachdb不包括參數(shù)@whereand

3.使用舉例:

--統(tǒng)計(jì)數(shù)據(jù)庫(kù)里每個(gè)表的詳細(xì)情況:
exec sp_MSforeachtable @command1="sp_spaceused '?'"

--獲得每個(gè)表的記錄數(shù)和容量:

 
 
 
  1. EXEC sp_MSforeachtable @command1="print '?'",  
  2. @command2="sp_spaceused '?'",  
  3. @command3= "SELECT count(*) FROM ? " 

--獲得所有的數(shù)據(jù)庫(kù)的存儲(chǔ)空間:

 
 
 
  1. EXEC sp_MSforeachdb @command1="print '?'",  
  2. @command2="sp_spaceused " 

--檢查所有的數(shù)據(jù)庫(kù)

 
 
 
  1. EXEC sp_MSforeachdb @command1="print '?'",  
  2. @command2="DBCC CHECKDB (?) " 

--更新PUBS數(shù)據(jù)庫(kù)中已t開頭的所有表的統(tǒng)計(jì):

 
 
 
  1. EXEC sp_MSforeachtable @whereand="and name like 't%'",  
  2. @replacechar='*',  
  3. @precommand="print 'Updating Statistics.....' print ''",  
  4. @command1="print '*' update statistics * ",  
  5. @postcommand= "print''print 'Complete Update Statistics!'" 

--刪除當(dāng)前數(shù)據(jù)庫(kù)所有表中的數(shù)據(jù)

 
 
 
  1. sp_MSforeachtable @command1='Delete from ?' 
  2. sp_MSforeachtable @command1 = "TRUNCATE TABLE ?" 

4.參數(shù)@whereand的用法:

@whereand參數(shù)在存儲(chǔ)過(guò)程中起到指令條件限制的作用,具體的寫法如下:
@whereend,可以這么寫 @whereand=' AND o.name in (''Table1'',''Table2'',.......)'
例如:我想更新Table1/Table2/Table3中NOTE列為NULL的值
sp_MSforeachtable @command1='Update ? Set NOTE='''' Where NOTE is NULL',@whereand=' AND o.name in (''Table1'',''Table2'',''Table3'')'

5."?"在存儲(chǔ)過(guò)程的特殊用法,造就了這兩個(gè)功能強(qiáng)大的存儲(chǔ)過(guò)程.

這里"?"的作用,相當(dāng)于DOS命令中、以及我們?cè)赪INDOWS下搜索文件時(shí)的通配符的作用。

6.小結(jié)

有了上面的分析,我們可以建立自己的sp_MSforeachObject:(轉(zhuǎn)貼)

 
 
 
  1. USE MASTER  
  2. GO  
  3. CREATE proc sp_MSforeachObject  
  4. @objectType int=1,  
  5. @command1 nvarchar(2000),   
  6. @replacechar nchar(1) = N'?',   
  7. @command2 nvarchar(2000) = null,  
  8. @command3 nvarchar(2000) = null,   
  9. @whereand nvarchar(2000) = null,  
  10. @precommand nvarchar(2000) = null,   
  11. @postcommand nvarchar(2000) = null  
  12. as  
  13. /* This proc returns one or more rows for each table (optionally, matching @where), with each table defaulting to its   
  14. own result set */  
  15. /* @precommand and @postcommand may be used to force a single result set via a temp table. */  
  16. /* Preprocessor won't replace within quotes so have to use str(). */  
  17. declare @mscat nvarchar(12)  
  18. select @mscat = ltrim(str(convert(int, 0x0002)))  
  19. if (@precommand is not null)  
  20. exec(@precommand)  
  21. /* Defined @isobject for save object type */  
  22. Declare @isobject varchar(256)  
  23. select @isobject= case @objectType when 1 then 'IsUserTable'  
  24. when 2 then 'IsView'  
  25. when 3 then 'IsTrigger'  
  26. when 4 then 'IsProcedure'   
  27. when 5 then 'IsDefault'   
  28. when 6 then 'IsForeignKey'  
  29. when 7 then 'IsScalarFunction'  
  30. when 8 then 'IsInlineFunction'  
  31. when 9 then 'IsPrimaryKey'  
  32. when 10 then 'IsExtendedProc'   
  33. when 11 then 'IsReplProc'  
  34. when 12 then 'IsRule'  
  35. end  
  36. /* Create the select */  
  37. /* Use @isobject variable isstead of IsUserTable string */  
  38. EXEC(N'declare hCForEach cursor global for select ''['' + REPLACE(user_name(uid), N'']'', N'']]'') + '']'' + ''.'' + ''['' +   
  39. REPLACE(object_name(id), N'']'', N'']]'') + '']'' from dbo.sysobjects o '  
  40. + N' where OBJECTPROPERTY(o.id, N'''+@isobject+''') = 1 '+N' and o.category & ' + @mscat + N' = 0 '  
  41. + @whereand)  
  42. declare @retval int  
  43. select @retval = @@error  
  44. if (@retval = 0)  
  45. exec @retval = sp_MSforeach_worker @command1, @replacechar, @command2, @command3  
  46. if (@retval = 0 and @postcommand is not null)  
  47. exec(@postcommand)  
  48. return @retval  
  49. GO 

這樣我們來(lái)測(cè)試一下:
--獲得所有的存儲(chǔ)過(guò)程的腳本:

 
 
 
  1. EXEc sp_MSforeachObject @command1="sp_helptext '?' ",@objectType=4 

--獲得所有的視圖的腳本:

 
 
 
  1. EXEc sp_MSforeachObject @command1="sp_helptext '?' ",@objectType=2 

--比如在開發(fā)過(guò)程中,沒一個(gè)用戶都是自己的OBJECT OWNER,所以在真實(shí)的數(shù)據(jù)庫(kù)時(shí)都要改為DBO:

 
 
 
  1. EXEc sp_MSforeachObject @command1="sp_changeobjectowner '?', 'dbo'",@objectType=1 
  2. EXEc sp_MSforeachObject @command1="sp_changeobjectowner '?', 'dbo'",@objectType=2 
  3. EXEc sp_MSforeachObject @command1="sp_changeobjectowner '?', 'dbo'",@objectType=3 
  4. EXEc sp_MSforeachObject @command1="sp_changeobjectowner '?', 'dbo'",@objectType=4 

這樣就非常方便的將每一個(gè)數(shù)據(jù)庫(kù)對(duì)象改為DBO。

關(guān)于SQL Server數(shù)據(jù)庫(kù)的兩個(gè)存儲(chǔ)過(guò)程:sp_MSforeachtable/sp_MSforeachdb的知識(shí)就介紹到這里了,希望本次的介紹能夠?qū)δ兴斋@!

【編輯推薦】

  1. SQL Server數(shù)據(jù)庫(kù)存儲(chǔ)過(guò)程的異常處理
  2. SQL Server數(shù)據(jù)庫(kù)利用SQL語(yǔ)句使用事務(wù)詳解
  3. SQL Server數(shù)據(jù)庫(kù)ROW_NUMBER()函數(shù)使用詳解
  4. 關(guān)閉SQL Server 2005遠(yuǎn)程連接以及其它對(duì)外服務(wù)
  5. SQL Server數(shù)據(jù)庫(kù)DATEDIFF的參數(shù)介紹及使用示例

標(biāo)題名稱:存儲(chǔ)過(guò)程:sp_MSforeachtable/sp_MSforeachdb
文章地址:http://m.5511xx.com/article/copppss.html