博客
关于我
对SQL数据表和数据库进行迭代操作
阅读量:428 次
发布时间:2019-03-06

本文共 1149 字,大约阅读时间需要 3 分钟。

以下是优化后的内容:

本文将介绍SQL Server中两个实用但未在官方在线教科书中提到的存储过程。这些过程在处理特定任务时非常方便,例如判断存储空间大小、行数以及用户表索引等。第一个存储过程sp_MSForEachDB可对感兴趣的服务器上的每个数据库执行三条命令。@command1、@command2和@command3分别指定要执行的命令,而@precommand和@postcommand则用于循环开始和结束时执行的操作。

需要注意的是,当使用sp_MSForEachDB执行命令时,每个命令集会作为一个批处理对数据库进行处理,即使命令集中只包含一条命令也会这样处理。这对于将捕获结果输出到文本而不是标准结果集表非常有用。要实现这一功能,可以通过菜单中的“查询”按钮,选择“输出结果”并设置为“输出到文本”或使用快捷键[Ctrl]T。

以下是一个示例代码:exec sp_MSForEachDB@command1 = "use ? exec sp_SpaceUsed"。运行该命令后,输出可能如下:

数据库名   数据库大小   未分配空间大小------------------ ------------------ ------------------master         5.25 MB   1.26 MBreserved data 2808 KB 1144 KB 1080 KB 584 KB

第二个存储过程sp_MSForEachTable接受7个参数:@command1、@replacechar、@command2、@command3、@whereand、@precommand和@postcommand。@replacechar用于替换问号,@whereand则用于添加Where或OrderBy条件。通过对参数进行命名,可以跳过传递空值的需求。

以下是一个示例:exec sp_MSForEachTable@command1 = "Print '?'", @command2 = "select count(*) from ?", @whereand = "ORDER BY 1"。运行该命令后,输出结果如下:

[HumanResources].[Department]-----------16[HumanResources].[Employee]-----------290[HumanResources].[EmployeeAddress]-----------290[HumanResources].[EmployeeDepartmentHistory]-----------296

这些存储过程在实际应用中非常实用,能够帮助数据库管理员高效处理日常任务。

转载地址:http://tbruz.baihongyu.com/

你可能感兴趣的文章
org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
查看>>
org.hibernate.HibernateException: Unable to get the default Bean Validation factory
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>
org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded
查看>>
org.tinygroup.serviceprocessor-服务处理器
查看>>
org/eclipse/jetty/server/Connector : Unsupported major.minor version 52.0
查看>>
org/hibernate/validator/internal/engine
查看>>
SQL-36 创建一个actor_name表,将actor表中的所有first_name以及last_name导入改表。
查看>>
orm总结
查看>>
os.path.join、dirname、splitext、split、makedirs、getcwd、listdir、sep等的用法
查看>>
os.system 在 Python 中不起作用
查看>>
OSCACHE介绍
查看>>
SQL--合计函数(Aggregate functions):avg,count,first,last,max,min,sum
查看>>
OSChina 周四乱弹 ——程序员为啥要买苹果手机啊?
查看>>
OSError: no library called “cairo-2“ was foundno library called “cairo“ was foundno library called
查看>>
OSG学习:几何体的操作(二)——交互事件、Delaunay三角网绘制
查看>>
OSG学习:几何对象的绘制(三)——几何元素的存储和几何体的绘制方法
查看>>
OSG学习:几何对象的绘制(二)——简易房屋
查看>>
OSG学习:场景图形管理(一)——视图与相机
查看>>