SQL Server有哪些隐藏特性?

例如,没有文档的系统存储过程,做一些非常有用但没有足够文档的事情的技巧?


答案

感谢大家的精彩回答!

存储过程

sp_msforeachtable: Runs a command with '?' replaced with each table name (v6.5 and up) sp_msforeachdb: Runs a command with '?' replaced with each database name (v7 and up) sp_who2: just like sp_who, but with a lot more info for troubleshooting blocks (v7 and up) sp_helptext: If you want the code of a stored procedure, view & UDF sp_tables: return a list of all tables and views of database in scope. sp_stored_procedures: return a list of all stored procedures xp_sscanf: Reads data from the string into the argument locations specified by each format argument. xp_fixeddrives:: Find the fixed drive with largest free space sp_help: If you want to know the table structure, indexes and constraints of a table. Also views and UDFs. Shortcut is Alt+F1

片段

Returning rows in random order All database User Objects by Last Modified Date Return Date Only Find records which date falls somewhere inside the current week. Find records which date occurred last week. Returns the date for the beginning of the current week. Returns the date for the beginning of last week. See the text of a procedure that has been deployed to a server Drop all connections to the database Table Checksum Row Checksum Drop all the procedures in a database Re-map the login Ids correctly after restore Call Stored Procedures from an INSERT statement Find Procedures By Keyword Drop all the procedures in a database Query the transaction log for a database programmatically.

功能

哈希字节() EncryptByKey 枢轴命令

Misc

Connection String extras TableDiff.exe Triggers for Logon Events (New in Service Pack 2) Boosting performance with persisted-computed-columns (pcc). DEFAULT_SCHEMA setting in sys.database_principles Forced Parameterization Vardecimal Storage Format Figuring out the most popular queries in seconds Scalable Shared Databases Table/Stored Procedure Filter feature in SQL Management Studio Trace flags Number after a GO repeats the batch Security using schemas Encryption using built in encryption functions, views and base tables with triggers


当前回答

SQL Server 2005:

select * from sys.dm_os_performance_counters

select * from sys.dm_exec_requests

其他回答

这不是一个隐藏的功能,而是在管理工作室的工具\选项\键盘下设置键映射: Alt+F1默认为sp_help“选定的文本”,但我不能没有添加Ctrl+F1为sp_helptext“选定的文本”

EXCEPT和INTERSECT

在比较两个查询结果时,这两个关键字是一种更优雅的简写和可读的表达查询意图的方式,而不是编写复杂的连接和子查询。作为SQL Server 2005的新版本,它们有力地补充了在TSQL语言中已经存在多年的UNION。

EXCEPT、INTERSECT和UNION是集合论中的基本概念,集合论是所有现代RDBMS使用的关系建模的基础和基础。现在,使用TSQL可以更直观、更容易地生成维恩图类型的结果。

根据单个语句中管道分隔的id字符串返回结果(替代传递xml或首先将分隔的字符串转换为表)

例子:

DECLARE @nvcIDs nvarchar(max)
SET @nvcIDs = '|1|2|3|'

SELECT C.*
FROM tblCompany C
WHERE @nvcIDs LIKE '%|' + CAST(C.CompanyID as nvarchar) + '|%' 

使用osql实用程序运行命令行查询/脚本/批处理

Stored proc sp_MSdependencies以一种比sp_depends更有用的方式告诉您对象依赖关系。对于某些产品版本,可以方便地暂时禁用子表约束,应用更改然后重新启用子表约束。这是查找依赖于父表的对象的好方法。

这段代码禁用了子表约束:

create table #deps
( oType int,
  oObjName sysname,
  oOwner nvarchar(200),
  oSequence int
)

insert into #deps  
exec sp_MSdependencies @tableName, null, 1315327

exec sp_MSforeachtable @command1 = 'ALTER TABLE ? NOCHECK CONSTRAINT ALL',
@whereand = ' and o.name in (select oObjName from #deps where oType = 8)'

应用更改后,可以运行以下代码重新启用约束:

exec sp_MSforeachtable @command1 = 'ALTER TABLE ? WITH CHECK CHECK CONSTRAINT ALL',
@whereand = ' and o.name in (select oObjName from #deps where oType = 8)'

第三个参数是@flags,它控制列出的依赖项类型。请阅读proc内容,了解如何根据自己的目的更改@flags。该过程使用位掩码来破译您想要返回的内容。