我正在使用master.sys.sp_MSforeachdb sp从多个数据库中提取数据。 select查询工作正常,但是当我尝试将其插入表中时,它会重新启动错误。如何将每个结果插入到我创建的表中?
Msg 245,Level 16,State 1,Line 4 将nvarchar值'master'转换为数据类型int时,转换失败。
EXECUTE master.sys.sp_MSforeachdb 'USE [?];
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
Insert INTO Connstrings.dbo.Results(host_name,user_name,program_name,request_duration_ms,[database])
select s.host_name, s.original_login_name AS user_name, s.program_name,
(DATEDIFF (ms,last_request_end_time, last_request_start_time )) AS request_duration_ms, DB_NAME() AS [database]
FROM sys.dm_exec_requests r
join sys.dm_exec_sessions s
on r.session_id = s.session_id'
答案 0 :(得分:0)
Connstrings.dbo.Results表中的[database]
列的类型为INT
。更改表定义:
alter table Results alter column [database] sysname
或更改查询:
EXECUTE master.sys.sp_MSforeachdb 'USE [?];
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
Insert INTO Connstrings.dbo.Results(host_name,user_name,program_name,request_duration_ms,[database])
select s.host_name, s.original_login_name AS user_name, s.program_name,
(DATEDIFF (ms,last_request_end_time, last_request_start_time )) AS request_duration_ms, DB_ID() AS [database]
FROM sys.dm_exec_requests r
join sys.dm_exec_sessions s
on r.session_id = s.session_id'