程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> 其他數據庫知識 >> MSSQL >> 體系存儲進程sp_MSforeachtable和sp_MSforeachdb應用解釋

體系存儲進程sp_MSforeachtable和sp_MSforeachdb應用解釋

編輯:MSSQL

體系存儲進程sp_MSforeachtable和sp_MSforeachdb應用解釋。本站提示廣大學習愛好者:(體系存儲進程sp_MSforeachtable和sp_MSforeachdb應用解釋)文章只能為提供參考,不一定能成為您想要的結果。以下是體系存儲進程sp_MSforeachtable和sp_MSforeachdb應用解釋正文


1.簡介:
作為DBA會常常須要檢討一切的數據庫或用戶表,好比:檢討一切數據庫的容量;看看指定命據庫一切用戶表的容量,一切表的記載數...,我們普通處置如許的成績都是用游標分離處置處置,好比:在數據庫檢索效力異常慢時,我們想檢討數據庫一切的用戶表,我們就必需經由過程寫游標來到達請求;假如我們用sp_MSforeachtable便可以異常便利的到達雷同的目標:EXEC sp_MSforeachtable @command1="print '?' DBCC CHECKTABLE ('?')"
體系存儲進程sp_MSforeachtable和sp_MSforeachdb,是微軟供給的兩個不地下的存儲進程,從mssql6.5開端。寄存在SQL Server的MASTER數據庫中。可以用來對某個數據庫的一切表或某個SQL辦事器上的一切數據庫停止治理,前面將對此停止具體引見。

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 --履行指令後的操作(相似控件的觸發後的操作)

今後為sp_MSforeachtable的參數,sp_MSforeachdb不包含參數@whereand

3.應用舉例:

--統計數據庫裡每一個表的具體情形:
exec sp_MSforeachtable @command1="sp_spaceused '?'"

--取得每一個表的記載數和容量:
EXEC sp_MSforeachtable @command1="print '?'",
@command2="sp_spaceused '?'",
@command3= "SELECT count(*) FROM ? "

--取得一切的數據庫的存儲空間:
EXEC sp_MSforeachdb @command1="print '?'",
@command2="sp_spaceused "

--檢討一切的數據庫
EXEC sp_MSforeachdb @command1="print '?'",
@command2="DBCC CHECKDB (?) "

--更新PUBS數據庫中已t開首的一切表的統計:
EXEC sp_MSforeachtable @whereand="and name like 't%'",
@replacechar='*',
@precommand="print 'Updating Statistics.....' print ''",
@command1="print '*' update statistics * ",
@postcommand= "print''print 'Complete Update Statistics!'"

--刪除以後數據庫一切表中的數據
sp_MSforeachtable @command1='Delete from ?'
sp_MSforeachtable @command1 = "TRUNCATE TABLE ?"

4.參數@whereand的用法:


@whereand參數在存儲進程中起到指令前提限制的感化,詳細的寫法以下:
@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."?"在存儲進程的特別用法,培養了這兩個功效壯大的存儲進程.

這裡"?"的感化,相當於DOS敕令中、和我們在WINDOWS下搜刮文件時的通配符的感化。

6.小結


有了下面的剖析,我們可以樹立本身的sp_MSforeachObject:(轉貼)
USE MASTER
GO
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

如許我們來測試一下:
--取得一切的存儲進程的劇本:
EXEc sp_MSforeachObject @command1="sp_helptext '?' ",@objectType=4
--取得一切的視圖的劇本:
EXEc sp_MSforeachObject @command1="sp_helptext '?' ",@objectType=2
--好比在開辟進程中,沒一個用戶都是本身的OBJECT OWNER,所以在真實的數據庫時都要改成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
如許就異常便利的將每個數據庫對象改成DBO.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved