转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 数据库 >> Sql Server >> 正文
浅析SQL SERVER一个没有公开的存储过程         

浅析SQL SERVER一个没有公开的存储过程

作者:闵涛 文章来源:闵涛的学习笔记 点击数:2485 更新时间:2007/11/14 10:59:52

浅析SQL SERVER一个没有公开的存储过程

   从SQLSERVER6.5开始,MS提供了一个非常有用的系统存储过程sp_MSforeachtable和sp_MSforeachDB;作为DBA会经常需要检查所有的数据库或用户表,比如:检查所有数据库的容量;看看指定数据库所有用户表的容量,所有表的记录数...,我们一般处理这样的问题都是用游标分别处理处理,比如:在数据库检索效率非常慢时,我们想检查数据库所有的用户表,我们就必须这样写游标:
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

这个系统存储过程有7个参数:
 @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, --在表前执行的指令
 @postcommand nvarchar(2000) = null --在表后执行的指令


所以上面的语句也可以这样写:
EXEC sp_MSforeachtable @command1="print ''''?''''",
         @command2= "DBCC CHECKTABLE(''''?'''')"

了解参数以后,就让我们做几个实列吧:
1.获得每个表的记录数和容量:
EXEC sp_MSforeachtable @command1="print ''''?''''",
         @command2="sp_spaceused ''''?''''",
         @command3= "SELECT count(*) FROM ? "

2.更新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_MSforeachDB除了@whereand外,和sp_MSforeachtable的参数是一样的,我们可以通过这个存储过程检测所有的数据库,比如:
1.检查所有的数据库
       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 hC

[1] [2]  下一页


[聊天工具]企业邮件系统的利器----FoxMail Server  [系统软件]OPEN SERVER 5.0.5安装EXP300阵列柜
[系统软件]关于Windows2000Server的灾难恢复  [常用软件][网络]下载服务革命性风暴Poco Server评测
[C语言系列]动态创建SQL Server数据库、表、存储过程等架构信…  [C语言系列]SQL Server到DB2连接服务器的实现
[C语言系列]SQL Server到SYBASE连接服务器的实现  [C语言系列]SQL Server到SQLBASE连接服务器的实现
[C语言系列]SQL Server连接VFP数据库的实现  [C语言系列]ASP+SQL Server之图象数据处理
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Sql Server  · MySql
    · Access  · ORACLE
    · SyBase  · 其他
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台