SELECT sp_WhoIsActive 到表中

Fra*_*ani 1 t-sql sql-server stored-procedures whois sql-agent-job

我正在尝试将sp_WhoIsActive编写到表中。目标是每 10 秒向表提供一次代理作业。

我遵循了这个指南,并尝试以这种方式为桌子提供食物:

--Log activity into table.  
DECLARE @destination_table VARCHAR(4000) = 
  '[Monitoring].[dbo].[WhoIsActive] ' 
  
EXEC sp_WhoIsActive
  @get_plans = 1, 
  @get_transaction_info = 1, 
  @destination_table = @destination_table;
Run Code Online (Sandbox Code Playgroud)

但结果我收到错误:

Warning: The join order has been enforced because a local join hint is used.
Msg 50000, Level 16, State 1, Procedure sp_WhoIsActive, Line 1111 [Batch Start Line 0]
Destination table not properly formatted.
Run Code Online (Sandbox Code Playgroud)

在 Google 上,我发现许多指南都在讨论一种解决方案,该解决方案可以帮助我在临时表中执行存储过程,然后我可以从那里创建一个表:

sp_configure 'Show Advanced Options', 1
GO
RECONFIGURE
GO
sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE
GO

SELECT * INTO #MyTempTable FROM OPENROWSET('SQLNCLI', 'Server=localhost;Trusted_Connection=yes;',
   'EXEC sp_WhoIsActive')

SELECT * FROM #MyTempTable
Run Code Online (Sandbox Code Playgroud)

但这个过程也因错误而失败:

Msg 11526, Level 16, State 1, Procedure sys.sp_describe_first_result_set, Line 1 [Batch Start Line 12]
The metadata could not be determined because statement 'INSERT #sessions
        (
            recursion,
            session_id,
            request_id' in procedure 'sp_WhoIsActive' uses a temp table.
Run Code Online (Sandbox Code Playgroud)

我尝试关注Kendra Little 博客,但这也不起作用。

最后我手动编写了表格:

CREATE TABLE [dbo].[WhoIsActive](
    [dd_hh_mm_ss_mss] [nvarchar](50) NOT NULL,
    [session_id] [tinyint] NOT NULL,
    [sql_text] [nvarchar](max) NOT NULL,
    [sql_command] [nvarchar](400) NOT NULL,
    [login_name] [nvarchar](50) NOT NULL,
    [wait_info] [nvarchar](50) NOT NULL,
    [tran_log_writes] [nvarchar](50) NOT NULL,
    [CPU] [smallint] NOT NULL,
    [tempdb_allocations] [smallint] NOT NULL,
    [tempdb_current] [smallint] NOT NULL,
    [blocking_session_id] [nvarchar](50) NOT NULL,
    [reads] [int] NOT NULL,
    [writes] [float] NOT NULL,
    [physical_reads] [tinyint] NOT NULL,
    [query_plan] [nvarchar](50) NOT NULL,
    [used_memory] [tinyint] NOT NULL,
    [status] [nvarchar](50) NOT NULL,
    [tran_start_time] [datetime2](7) NOT NULL,
    [implicit_tran] [nvarchar](50) NOT NULL,
    [open_tran_count] [tinyint] NOT NULL,
    [percent_complete] [nvarchar](50) NOT NULL,
    [host_name] [nvarchar](50) NOT NULL,
    [database_name] [nvarchar](50) NOT NULL,
    [program_name] [nvarchar](100) NOT NULL,
    [start_time] [datetime2](7) NOT NULL,
    [login_tine] [datetime2](7) NOT NULL,
    [request_id] [tinyint] NOT NULL,
    [collection_time] [datetime2](7) NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
Run Code Online (Sandbox Code Playgroud)

但这也失败了,我无法为餐桌提供工作。

sp_WhoIsActive如此受欢迎,我不敢相信我是唯一一个尝试将结果插入表中的人。

Stu*_*Stu 8

您需要创建一个适合与过程的输出一起使用的表,其架构可能会根据您使用的选项而有所不同。

SP_WhoIsActive实际上会为您提供create脚本,因此要捕获默认选项,只需执行以下操作

declare @definition varchar(max)
exec sp_WhoIsActive @return_schema = 1, @schema = @definition output
print @definition
Run Code Online (Sandbox Code Playgroud)

这将返回适当的 T-SQL:

CREATE TABLE < table_name > (
    [dd hh:mm:ss.mss] VARCHAR(8000) NULL
    ,[session_id] SMALLINT NOT NULL
    ,[sql_text] XML NULL
    ,[login_name] NVARCHAR(128) NOT NULL
    ,[wait_info] NVARCHAR(4000) NULL
    ,[CPU] VARCHAR(30) NULL
    ,[tempdb_allocations] VARCHAR(30) NULL
    ,[tempdb_current] VARCHAR(30) NULL
    ,[blocking_session_id] SMALLINT NULL
    ,[reads] VARCHAR(30) NULL
    ,[writes] VARCHAR(30) NULL
    ,[physical_reads] VARCHAR(30) NULL
    ,[used_memory] VARCHAR(30) NULL
    ,[status] VARCHAR(30) NOT NULL
    ,[open_tran_count] VARCHAR(30) NULL
    ,[percent_complete] VARCHAR(30) NULL
    ,[host_name] NVARCHAR(128) NULL
    ,[database_name] NVARCHAR(128) NULL
    ,[program_name] NVARCHAR(128) NULL
    ,[start_time] DATETIME NOT NULL
    ,[login_time] DATETIME NULL
    ,[request_id] INT NULL
    ,[collection_time] DATETIME NOT NULL
    )
Run Code Online (Sandbox Code Playgroud)

使用所需的目标表名称编辑并运行,然后可以sp_whoisactive使用目标表选项运行

exec sp_WhoIsActive @destination_table='Monitoring.dbo.WhoIsActive'
Run Code Online (Sandbox Code Playgroud)

请参阅文档