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


当前回答

SQLCMD

如果您有脚本需要反复运行,但必须更改轻微的细节,那么在sqlcmd模式下运行ssms非常棒。sqlcmd命令行也非常漂亮。

我最喜欢的特点是:

你需要设置变量。适当的变量,不需要跳过sp_exec圈 您可以一个接一个地运行多个脚本 这些脚本可以引用“外部”脚本中的变量

Red Gate的Simpletalk没有再滔滔滔滔地说什么,而是对sqlcmd - sqlcmd工作台做了一个很棒的总结。Donabel Santos也有一些很棒的SQLCMD示例。

其他回答

在Management Studio中,你可以在GO批处理结束标记后面放一个数字,以使批处理重复该次数:

PRINT 'X'
GO 10

将打印'X' 10次。这可以让你在做重复的事情时避免繁琐的复制/粘贴。

我不确定这是否是一个隐藏的功能,但我偶然发现了这个功能,并发现它在很多情况下都很有用。您可以在单个选择语句中连接一组字段,而不是使用游标并遍历选择语句。

例子:

DECLARE @nvcConcatonated nvarchar(max)
SET @nvcConcatonated = ''

SELECT @nvcConcatonated = @nvcConcatonated + C.CompanyName + ', '
FROM tblCompany C
WHERE C.CompanyID IN (1,2,3)

SELECT @nvcConcatonated

结果:

Acme, Microsoft, Apple,

强制参数化

参数化允许SQL Server利用查询计划重用,并避免后续执行类似查询时的编译和优化开销。然而,由于这样或那样的原因,仍然有许多应用程序存在特殊的查询编译开销。对于查询编译次数较多、降低CPU利用率和响应时间对工作负载至关重要的情况,强制参数化可以提供帮助。

Link

以下是我最喜欢的一些东西:

在sp2 -工具/选项/脚本下增加了脚本选项

使用模式的新安全性—创建两个模式:user_access、admin_access。把你的用户过程放在一个,你的管理过程放在另一个,像这样:user_access。showList, admin_access.deleteUser。将模式上的EXECUTE授权给你的应用用户/角色。不再一直授予EXECUTE。

使用内置加密函数、视图(为了表示而解密)和带触发器的基表(在插入/更新时加密)进行加密。

行构造函数

您可以用一个插入语句插入多行数据。

INSERT INTO Colors (id, Color)
VALUES (1, 'Red'),
       (2, 'Blue'),
       (3, 'Green'),
       (4, 'Yellow')