我们的 Azure SQL 托管实例数据库存在问题,正在尝试收集一些统计信息以帮助我们解决问题。因此,我创建了一个名为 [dbo].[WhoIsActive] 的表,其架构如下:
CREATE TABLE [dbo].[WhoIsActive](
[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,
--[tran_log_writes] [nvarchar](4000) 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,
--[query_plan] [xml] NULL,
[status] [varchar](30) NOT NULL,
--[tran_start_time] [datetime] 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
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
/****** Object: Index [cx_collection_time] Script Date: 2/28/2023 7:17:59 AM ******/
CREATE CLUSTERED INDEX [cx_collection_time] ON [dbo].[WhoIsActive]
(
[collection_time] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
GO
然后我从https://github.com/amachanic/sp_whoisactive/releases
创建了一个广为人知的存储过程sp_whoisactive最新版本下一步是定期将过程的结果插入表中。
选项1: 我尝试过这样做:
Insert Into [dbo].[WhoIsActive]
Exec sp_whoisactive
但出现此错误:
消息 8164,级别 16,状态 1,过程 sp_whoisactive,第 3737 行 [批量开始行 44]
INSERT EXEC 语句不能嵌套。*
选项2:我尝试过以下方法:
SET NOCOUNT ON;
DECLARE @retention INT = 60,
@destination_table VARCHAR(500) = 'WhoIsActive',
@destination_database sysname = 'DBATools',
@schema VARCHAR(MAX),
@SQL NVARCHAR(4000),
@parameters NVARCHAR(500),
@exists BIT;
SET @destination_table = @destination_database + '.dbo.' + @destination_table;
--create the logging table
IF OBJECT_ID(@destination_table) IS NULL
BEGIN;
EXEC dbo.sp_WhoIsActive @get_transaction_info = 1,
@get_outer_command = 1,
@get_plans = 1,
@return_schema = 1,
@schema = @schema OUTPUT;
SET @schema = REPLACE(@schema, '<table_name>', @destination_table);
EXEC ( @schema );
END;
--create index on collection_time
SET @SQL
= 'USE ' + QUOTENAME(@destination_database)
+ '; IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(@destination_table) AND name = N''cx_collection_time'') SET @exists = 0';
SET @parameters = N'@destination_table varchar(500), @exists bit OUTPUT';
EXEC sys.sp_executesql @SQL, @parameters, @destination_table = @destination_table, @exists = @exists OUTPUT;
IF @exists = 0
BEGIN;
SET @SQL = 'CREATE CLUSTERED INDEX cx_collection_time ON ' + @destination_table + '(collection_time ASC)';
EXEC ( @SQL );
END;
--collect activity into logging table
EXEC dbo.sp_WhoIsActive @get_transaction_info = 1,
@get_outer_command = 1,
@get_plans = 1,
--@destination_table = [WhoIsActive];
@destination_table = @destination_table;
--purge older data
SET @SQL
= 'DELETE FROM ' + @destination_table + ' WHERE collection_time < DATEADD(day, -' + CAST(@retention AS VARCHAR(10))
+ ', GETDATE());';
EXEC ( @SQL );
我现在收到此错误消息:
消息 213,第 16 级,状态 1,第 1 行
列名称或提供的值的数量与表定义不匹配。
所以我的问题是,将这些数据存储到表中的最佳方法是什么?还是我哪里做错了?
你只需要添加以下参数并执行,它就会创建一个“CREATE Table”语句:在顶部添加:DECLARE @Schema VARCHAR(MAX); EXEC sp_whoisactive... 在此过程中使用您喜欢的任何格式,但最后添加以下变量: @return_schema = 1, @SCHEMA = @schema 输出 打印@Schema
然后当你执行它来记录到你的表时,添加这个参数:
@destination_table = '.dbo.