博客
关于我
对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.dubbo.common.serialize.SerializationException: com.alibaba.fastjson2.JSONException: not s
查看>>
sqlserver学习笔记(三)—— 为数据库添加新的用户
查看>>
org.apache.http.conn.HttpHostConnectException: Connection to refused
查看>>
org.apache.ibatis.binding.BindingException: Invalid bound statement错误一例
查看>>
org.apache.ibatis.exceptions.PersistenceException:
查看>>
org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
查看>>
org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
查看>>
org.apache.poi.hssf.util.Region
查看>>
org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
查看>>
org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
查看>>
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:processDebugManifest'
查看>>
org.hibernate.HibernateException: Unable to get the default Bean Validation factory
查看>>
org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
查看>>
org.springframework.amqp.AmqpConnectException:java.net.ConnectException:Connection timed out:connect
查看>>
org.springframework.beans.factory.BeanDefinitionStoreException
查看>>
org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata
查看>>
org.springframework.boot:spring boot maven plugin丢失---SpringCloud Alibaba_若依微服务框架改造_--工作笔记012
查看>>
SQL-CLR 类型映射 (LINQ to SQL)
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>