将sp_who2的输出存储在表中


8

我正在寻找类似的东西,我可以将sp_who2的输出存储在一个表中,而不必先创建表:

create table test as exec sp_who2

您正在使用哪个版本的SQL Server?
亚伦·伯特兰

Answers:



17
USE yourDb;

CREATE TABLE temp_sp_who2
    (
      SPID INT,
      Status VARCHAR(1000) NULL,
      Login SYSNAME NULL,
      HostName SYSNAME NULL,
      BlkBy SYSNAME NULL,
      DBName SYSNAME NULL,
      Command VARCHAR(1000) NULL,
      CPUTime INT NULL,
      DiskIO BIGINT NULL, -- int
      LastBatch VARCHAR(1000) NULL,
      ProgramName VARCHAR(1000) NULL,
      SPID2 INT
      , RequestId INT NULL --comment out for SQL 2000 databases

    )


INSERT  INTO temp_sp_who2
EXEC sp_who2

SELECT  *
FROM    temp_sp_who2
WHERE   DBName = 'yourDb'

链接

刚刚在SQL Server 2012上进行了测试


3

Mihai解决方案的扩展适用于任何想要随时间捕获此问题的人,例如跟踪服务器上的活动:

IF OBJECT_ID('dbo.temp_sp_who2', 'U') IS NOT NULL 
BEGIN
  DROP TABLE dbo.temp_sp_who2;
END; 

CREATE TABLE temp_sp_who2
    (
      SPID INT
      ,Status VARCHAR(1000) NULL
      ,LOGIN SYSNAME NULL
      ,HostName SYSNAME NULL
      ,BlkBy SYSNAME NULL
      ,DBName SYSNAME NULL
      ,Command VARCHAR(1000) NULL
      ,CPUTime INT NULL
      ,DiskIO INT NULL
      ,LastBatch VARCHAR(1000) NULL
      ,ProgramName VARCHAR(1000) NULL
      ,SPID2 INT
      , RequestID INT NULL --comment out for SQL 2000 databases
      , InsertedDate datetime DEFAULT GETDATE()
    );

--This insert can be added to a job that runs periodically.
INSERT  INTO temp_sp_who2
    (
        SPID
        ,Status                                                                                                                                                                                                                                                           
        ,LOGIN                                                                                                                            
        ,HostName                                                                                                                         
        ,BlkBy                                                                                                                            
        ,DBName                                                                                                                           
        ,Command                                                                                                                                                                                                                                                          
        ,CPUTime     
        ,DiskIO      
        ,LastBatch                                                                                                                                                                                                                                                        
        ,ProgramName                                                                                                                                                                                                                                                      
        ,SPID2       
        ,RequestID   
    )
EXECUTE sp_who2;

SELECT  *
FROM    temp_sp_who2;

2

该问题要求提供一种无需创建表即可获取结果的方法。使用表变量可以做到这一点:

CREATE PROC [dbo].[sp_who2db] (@DBName VARCHAR(200))
AS
BEGIN

      DECLARE @who2 TABLE(
            [SPID] INT,
            [Status] VARCHAR(200),
            [Login] VARCHAR(200),
            [HostName] VARCHAR(200),
            [BlkBy] VARCHAR(20),
            [DBName] VARCHAR(200),
            [Command] VARCHAR(200),
            [CPUTime] BIGINT,
            [DiskIO] BIGINT,
            [LastBatch] VARCHAR(20),
            [ProgramName] VARCHAR(200),
            [SPID2] INT,
            [RequestID] INT
      )

      INSERT @who2
      EXEC sp_who2

      SELECT * FROM @who2 WHERE DBName = @DBName

END

筛选sp_who2通过数据库乔恩·埃杰顿

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.