国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
淺析SQL SERVER一個沒有公開的存儲過程
userphoto

2005.09.09

關注

淺析SQL SERVER一個沒有公開的存儲過程

[日期:2005-07-08]來源:CSDN  作者:[字體: ]

淺析SQL SERVER一個沒有公開的存儲過程

   從SQLSERVER6.5開始,MS提供了一個非常有用的系統(tǒng)存儲過程sp_MSforeachtable和sp_MSforeachDB;作為DBA會經(jīng)常需要檢查所有的數(shù)據(jù)庫或用戶表,比如:檢查所有數(shù)據(jù)庫的容量;看看指定數(shù)據(jù)庫所有用戶表的容量,所有表的記錄數(shù)...,我們一般處理這樣的問題都是用游標分別處理處理,比如:在數(shù)據(jù)庫檢索效率非常慢時,我們想檢查數(shù)據(jù)庫所有的用戶表,我們就必須這樣寫游標:
DECLARE @TableName varchar(255)
DECLARE @ExeSQL varchar(4000)

DECLARE Table_Cursor CURSOR FOR SELECT [name] FROM sysobjects WHERE xtype=‘U‘

OPEN Table_Cursor
FETCH NEXT FROM  Table_Cursor INTO @TableName

WHILE(@@FETCH_STATUS=0)
BEGIN
 PRINT @TableName
 SELECT @ExeSQL=‘DBCC CHECKTABLE(
‘‘‘+@TableName+‘‘‘)‘
 EXEC(@EXESQL)
FETCH NEXT FROM  Table_Cursor INTO @TableName
END

CLOSE Table_Cursor
DEALLOCATE Table_Cursor
GO

    如果我們用sp_MSforeachtable就可以非常方便的達到相同的目的:
EXEC sp_MSforeachtable @command1="print ‘?‘ DBCC CHECKTABLE(‘?‘)"
大家可以看出這樣就更加簡潔(雖然在后臺也是通過游標來處理的),下面我們就仔細分析一下sp_MSforeachtable這個存儲過程:

我們看看sp_MSforeachtable詳細的CODE:
USE MASTER
GO
SP_HELPTEXT sp_MSforeachtable

--下面時sp_MSforeachtable的原始代碼

CREATE proc sp_MSforeachtable
 @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)

 /* Create the select */
   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‘‘IsUserTable‘‘) = 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

這個系統(tǒng)存儲過程有7個參數(shù):
 @command1 nvarchar(2000),  --第一條運行的T-SQL指令
 @replacechar nchar(1) = N‘?‘,   --指定的占位符號
 @command2 nvarchar(2000) = null,--第二條運行的T-SQL指令
    @command3 nvarchar(2000) = null, --第三條運行的T-SQL指令
 @whereand nvarchar(2000) = null, --可選條件來選擇表
 @precommand nvarchar(2000) = null, --在表前執(zhí)行的指令
 @postcommand nvarchar(2000) = null --在表后執(zhí)行的指令


所以上面的語句也可以這樣寫:
EXEC sp_MSforeachtable @command1="print ‘?‘",
         @command2= "DBCC CHECKTABLE(‘?‘)"

了解參數(shù)以后,就讓我們做幾個實列吧:
1.獲得每個表的記錄數(shù)和容量:
EXEC sp_MSforeachtable @command1="print ‘?‘",
         @command2="sp_spaceused ‘?‘",
         @command3= "SELECT count(*) FROM ? "

2.更新PUBS數(shù)據(jù)庫中已t開頭的所有表的統(tǒng)計:
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_MSforeachDB除了@whereand外,和sp_MSforeachtable的參數(shù)是一樣的,我們可以通過這個存儲過程檢測所有的數(shù)據(jù)庫,比如:
1.檢查所有的數(shù)據(jù)庫
       EXEC sp_MSforeachdb  @command1="print ‘?‘",
                                           @command2="DBCC CHECKDB (?) "

有了上面的分析,我們可以建立自己的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
這樣我們來測試一下:
1.獲得所有的存儲過程的腳本:
         EXEc sp_MSforeachObject @command1="sp_helptext ‘?‘ ",@objectType=4
2.獲得所有的視圖的腳本:
         EXEc sp_MSforeachObject @command1="sp_helptext ‘?‘ ",@objectType=2
3.比如在開發(fā)過程中,沒一個用戶都是自己的OBJECT OWNER,所以在真實的數(shù)據(jù)庫時都要改為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
  這樣就非常方便的將每一個數(shù)據(jù)庫對象改為DBO.

當然還要很多非常好的功能,大家可以自己深入研究吧:-)

 

本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
sp_MSforeachtable 與 sp_MSforeachdb
【關于】sp_MSforeachtable 和sp_MSforeachDB
SQL Server未公開的兩個存儲過程 - 振河 - 博客園
阿D SQL注入工具常用的一些注入命令
SQL詳解
sqlserver存儲過程集錦(一)
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服