5个最常用的系统存储过程

There are a number of documented and undocumented system stored procedures in MS SQL Server that are helpful for every web developer working with databases. From the developer’s perspective, here’s a list of 5 System stored procedures that are my favorite.

1. sp_help

Purpose

sp_help gives information about database objects. If you wanted to quickly know the structure of a table but are too lazy to look for the schema diagram or to dig for the table you are interested in within the Object explorer, sp_help is here to help

Syntax

It can be used without parameters to get the information of objects in the database.

It can be used with a parameter to get the information of a particular object  

Examples

On the Adventure Works database:

Executing sp_help would yield the following recordset

image

Executing sp_help ‘Person.Address’ yields the following recordset

image

Note that it is not required to enclose the object name within single quotes unless the object name has a dot in it.

2. sp_helptext

Purpose

sp_help gives definition information of objects such as system stored procedures, user defined stored procedures, user defined functions, triggers etc.

Syntax

  1. sp_helptext <Name of the object>  
Example

On the Adventure Works database:

Executing a sp_helptext 'dbo.uspGetBillOfMaterials' yields the following definition of the user defined stored procedure

image

3. sp_MSforeachtable

Purpose

Caution – This is an undocument stored procedure and should not be relied on. It is not listed in SQL BOL and should be used at your own risk.

This is a very useful stored procedure for executing a command for ALL the tables in the database. Say you wanted to get the number of rows in all the tables in your database, you could write:

Example

  1. EXEC sp_MSforeachtable 'SELECT ''?'', COUNT(*) FROM ?'  
The literal ? is used as a token to replace the table name. The output for the Adventure Works database is shown below

image

For each table in the database, it would list the table name and the number of rows in that table.

Want to find out how much space is used by each table in your database. Try this:

  1. sp_MSforeachtable 'execute sp_spaceused @objname = ''?'' '  
More information on sp_spaceused later in this article.

4. sp_depends

Purpose

Ever wanted to make a change to a table but were not sure what other objects are dependent on this table? There could be views or stored procedures that could break due to this change. In situations like this, sp_depends come to the rescue.

Syntax

  1. sp_depends <Name of the object>  
Example

In the Adventure Works database, say I wanted to find out all the objects that are dependent on the Person.Address table.

By executing sp_depends 'Person.Address', the result set is as shown

image

5. sp_spaceused

Purpose

This is a simple stored procedure that gives information on the size of the database or the database objects

Syntax

If it is used without parameters, it would return the database information

  1. sp_spaceused  

If it is used with a parameter, it would return the information on the object

  1. sp_spaceused <Name of the object>  

Example

In the Adventure Works database, executing the sp_spaceused without parameters gives the following result

image

Executing the sp_spaceused ‘Person.Address’ (on a table object)

image

Conclusion

This article gives a brief description on 5 useful system stored procedures that developers can use on a daily basis. Happy coding!
  

转载于:https://www.cnblogs.com/simonhaninmelbourne/archive/2009/10/21/1587392.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值