SQL Server 未记录的存储过程和函数
我发现 SQL Server 中有许多高级功能存储过程,但联机丛书中没有记录这些存储过程。我通过谷歌搜索找到了它们。这里有一些,如果你还有其他的,请与我分享。
SQL Server
sp_checknames
sp_columns_rowset
sp_enumoledbdatasources
sp_fixindex
sp_gettypestring
sp_ms_marksystemobject
sp_msaddguidcolumn
sp_msaddguidindex
sp_msaddlogin_implicit_ntlogin
sp_msadduser_implicit_ntlogin
sp_mscheck_uid_owns_anything
sp_msdbuseraccess
sp_msdbuserpriv
sp_msdependencies
sp_msdrop_object
sp_msforeachdb
sp_msforeachtable
sp_msget_qualified_name
sp_msgettools_path
sp_msgetversion
sp_msguidtostr
sp_mshelpcolumns
sp_mshelpindex
sp_mshelptype
sp_msindexspace
sp_msis_pk_col
sp_mskilldb
sp_msloginmappings
sp_mstablekeys
sp_mstablerefs
sp_mstablespace
sp_msunc_to_drive
sp_msuniquecolname
sp_msuniquename
sp_msuniqueobjectname
sp_msuniquetempname
sp_tempdbspace
sp_who2
xp_delete_file
xp_dirtree
xp_enum_oledb_providers
xp_enumcodepages
xp_enumdsn
xp_enumerrorlogs
xp_enumgroups
xp_fileexist
xp_fixeddrives
xp_get_mapi_default_profile
xp_get_mapi_profiles
xp_getnetname
xp_qv
xp_readerrorlog
xp_regaddmultistring
xp_regdeletekey
xp_regdeletevalue
xp_regenumvalues
xp_regread
xp_regremovemultistring
xp_regwrite
xp_subdirs
xp_varbintohexstr
sp_MSforeachtable 中未记录的存储过程可用于循环访问数据库中的所有表。以下是这个有用的存储过程的一些常见用法
Display the size of all tables in a database
EXEC sp_MSforeachtable @command1="EXEC sp_spaceused '?'"
Display Number of Rows in all Tables in a database
EXEC sp_MSforeachtable 'SELECT ''?'', Count(*) as NumberOfRows FROM ?'
Rebuild all indexes of all tables in a database
EXEC sp_MSforeachtable @command1="print '?' DBCC DBREINDEX ('?', ' ', 80)"
GO
注意:DBCC DBREINDEX 在 SQL 2005 中已被弃用。微软表示“此功能将在 Microsoft SQL Server 的未来版本中删除。避免在新的开发工作中使用此功能,并计划修改当前使用此功能的应用程序。”
Disable all constraints of all tables in a database
USE YOURDBNAME
EXEC sp_MSforeachtable @command1="ALTER TABLE ? NOCHECK CONSTRAINT ALL"
Disable all Triggers of all tables in a database
USE YOURDBNAME
EXEC sp_MSforeachtable 'ALTER TABLE ? DISABLE TRIGGER ALL'
Delete all data from all tables in your database
-- disable referential integrity
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
GO
EXEC sp_MSForEachTable '
IF OBJECTPROPERTY(object_id(''?''), ''TableHasForeignRef'') = 1
DELETE FROM ?
else
TRUNCATE TABLE ?
'
GO
-- enable referential integrity again
EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
GO
To RESEED all table to 0, use this script
EXEC sp_MSForEachTable '
IF OBJECTPROPERTY(object_id(''?''), ''TableHasIdentity'') = 1
DBCC CHECKIDENT (''?'', RESEED, 0)
GO
人们在一个地方分享您的知识,因此许多开发人员都从中受益。
I found there are lots of advanced featured stored procedures in SQL Server which are not documented by the Books Online. I found them from googling. Here are some of them if you have others please share them with me.
Undocumented Stored Procedures in SQL Server
sp_checknames
sp_columns_rowset
sp_enumoledbdatasources
sp_fixindex
sp_gettypestring
sp_ms_marksystemobject
sp_msaddguidcolumn
sp_msaddguidindex
sp_msaddlogin_implicit_ntlogin
sp_msadduser_implicit_ntlogin
sp_mscheck_uid_owns_anything
sp_msdbuseraccess
sp_msdbuserpriv
sp_msdependencies
sp_msdrop_object
sp_msforeachdb
sp_msforeachtable
sp_msget_qualified_name
sp_msgettools_path
sp_msgetversion
sp_msguidtostr
sp_mshelpcolumns
sp_mshelpindex
sp_mshelptype
sp_msindexspace
sp_msis_pk_col
sp_mskilldb
sp_msloginmappings
sp_mstablekeys
sp_mstablerefs
sp_mstablespace
sp_msunc_to_drive
sp_msuniquecolname
sp_msuniquename
sp_msuniqueobjectname
sp_msuniquetempname
sp_tempdbspace
sp_who2
xp_delete_file
xp_dirtree
xp_enum_oledb_providers
xp_enumcodepages
xp_enumdsn
xp_enumerrorlogs
xp_enumgroups
xp_fileexist
xp_fixeddrives
xp_get_mapi_default_profile
xp_get_mapi_profiles
xp_getnetname
xp_qv
xp_readerrorlog
xp_regaddmultistring
xp_regdeletekey
xp_regdeletevalue
xp_regenumvalues
xp_regread
xp_regremovemultistring
xp_regwrite
xp_subdirs
xp_varbintohexstr
sp_MSforeachtable can be used to loop through all the tables in your databases. Here are some common usages of this useful stored procedure
Display the size of all tables in a database
EXEC sp_MSforeachtable @command1="EXEC sp_spaceused '?'"
Display Number of Rows in all Tables in a database
EXEC sp_MSforeachtable 'SELECT ''?'', Count(*) as NumberOfRows FROM ?'
Rebuild all indexes of all tables in a database
EXEC sp_MSforeachtable @command1="print '?' DBCC DBREINDEX ('?', ' ', 80)"
GO
Note: DBCC DBREINDEX has been deprecated in SQL 2005. Microsoft says "This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature. Use ALTER INDEX instead."
Disable all constraints of all tables in a database
USE YOURDBNAME
EXEC sp_MSforeachtable @command1="ALTER TABLE ? NOCHECK CONSTRAINT ALL"
Disable all Triggers of all tables in a database
USE YOURDBNAME
EXEC sp_MSforeachtable 'ALTER TABLE ? DISABLE TRIGGER ALL'
Delete all data from all tables in your database
-- disable referential integrity
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
GO
EXEC sp_MSForEachTable '
IF OBJECTPROPERTY(object_id(''?''), ''TableHasForeignRef'') = 1
DELETE FROM ?
else
TRUNCATE TABLE ?
'
GO
-- enable referential integrity again
EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
GO
To RESEED all table to 0, use this script
EXEC sp_MSForEachTable '
IF OBJECTPROPERTY(object_id(''?''), ''TableHasIdentity'') = 1
DBCC CHECKIDENT (''?'', RESEED, 0)
GO
guys share your knowledge at one place so lots of developers gain bani fits from this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论