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


当前回答

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。该过程使用位掩码来破译您想要返回的内容。

其他回答

以下是一些我认为有用但很多人似乎不知道的功能:

sp_tables

返回可以为的对象列表 在当前环境下查询。 这意味着任何对象都可以出现 在FROM子句中,同义词除外 对象。

Link

sp_stored_procedures

中存储过程的列表 当前环境。

Link

我用来将这个存储过程添加到主数据库,

改进:

修剪主机名,这样复制粘贴就可以在VNC上工作。 增加了一个LOCK选项,用于查看当前锁定的进程。

用法:

EXEC sp_who3 'ACTIVE' EXEC sp_who3 'LOCK' EXEC sp_who3 spid_No

就是这样。

CREATE procedure sp_who3
       @loginame sysname = NULL --or 'active' or 'lock'
as

declare  @spidlow   int,
         @spidhigh  int,
         @spid      int,
         @sid       varbinary(85)

select   @spidlow   =     0
        ,@spidhigh  = 32767


if @loginame is not NULL begin
    if upper(@loginame) = 'ACTIVE' begin
        select spid, ecid, status
            , loginame=rtrim(loginame)
            , hostname=rtrim(hostname)
            , blk=convert(char(5),blocked)
            , dbname = case
                            when dbid = 0 then null
                            when dbid <> 0 then db_name(dbid)
                        end
              ,cmd
        from  master.dbo.sysprocesses
        where spid >= @spidlow and spid <= @spidhigh AND
              upper(cmd) <> 'AWAITING COMMAND'
        return (0)
    end
    if upper(@loginame) = 'LOCK' begin
        select spid , ecid, status
            , loginame=rtrim(loginame)
            , hostname=rtrim(hostname)
            , blk=convert(char(5),blocked)
            , dbname = case
                            when dbid = 0 then null
                            when dbid <> 0 then db_name(dbid)
                        end
              ,cmd
        from  master.dbo.sysprocesses
        where spid >= 0 and spid <= 32767 AND
              upper(cmd) <> 'AWAITING COMMAND'
        AND convert(char(5),blocked) > 0
        return (0)
    end

end

if (@loginame is not NULL
   AND  upper(@loginame) <> 'ACTIVE'
   )
begin
    if (@loginame like '[0-9]%')    -- is a spid.
    begin
        select @spid = convert(int, @loginame)
        select spid, ecid, status
            , loginame=rtrim(loginame)
            , hostname=rtrim(hostname)
            , blk=convert(char(5),blocked)
            , dbname = case
                            when dbid = 0 then null
                            when dbid <> 0 then db_name(dbid)
                        end
              ,cmd
        from  master.dbo.sysprocesses
        where spid = @spid
    end
    else
    begin
        select @sid = suser_sid(@loginame)
        if (@sid is null)
        begin
            raiserror(15007,-1,-1,@loginame)
            return (1)
        end
        select spid, ecid, status
            , loginame=rtrim(loginame)
            , hostname=rtrim(hostname)
            , blk=convert(char(5),blocked)
            , dbname = case
                            when dbid = 0 then null
                            when dbid <> 0 then db_name(dbid)
                        end
               ,cmd
        from  master.dbo.sysprocesses
        where sid = @sid
    end
    return (0)
end


/* loginame arg is null */
select spid,
       ecid,
       status
       , loginame=rtrim(loginame)
       , hostname=rtrim(hostname)
       , blk=convert(char(5),blocked)
       , dbname = case
                    when dbid = 0 then null
                    when dbid <> 0 then db_name(dbid)
                end
       ,cmd
from  master.dbo.sysprocesses
where spid >= @spidlow and spid <= @spidhigh


return (0) -- sp_who

您可以用子查询创建一个逗号分隔的列表,并且后面没有最后一个逗号。据说这比之前使用的函数更有效。我想是2005年之后。

SELECT 
    Project.ProjectName,
    (SELECT
        SUBSTRING(
            (SELECT ', ' + Site.SiteName
            FROM Site
            WHERE Site.ProjectKey = Project.ProjectKey
            ORDER BY Project.ProjectName
    FOR XML PATH('')),2,200000)) AS CSV 
FROM Project

您还可以使用带有嵌套查询的FOR XML PATH来选择XML,我发现这很有用。

HashBytes()返回其输入的MD2、MD4、MD5、SHA或SHA1哈希值。

这里有一些未记录的命令:未记录但很方便的SQL server Procs和DBCC命令