xp_readerrorlog"参数类型无效"变量

时间:2015-09-18 18:32:49

标签: sql sql-server sql-server-2008-r2 sqlcode

我试图编写一些代码来从SQL 2008R2实例查询SQL错误日志。这就是我所拥有的:

declare @Start_Time  DATETIME = NULL;
declare @End_Time    DATETIME = NULL;

IF @Start_Time IS NULL 
  SELECT @Start_Time = GETDATE()-1
IF @End_Time   IS NULL 
  SELECT @End_Time   = GETDATE()

select @start_time start_time, CONVERT(VARCHAR(19),@start_time,120) conv_s_time
select @End_time   end_time,   CONVERT(VARCHAR(19),@End_time,120)   conv_e_time
EXEC xp_ReadErrorLog 0, 1, '', '', '2015-09-17 11:47:16', '2015-09-18 11:47:16' 
EXEC xp_ReadErrorLog 0, 1, '', '', ' + CONVERT(VARCHAR(19),@Start_Time,120) ' 
EXEC xp_ReadErrorLog 0, 1, '', '', ' + CONVERT(VARCHAR(19),@Start_Time,120) ' , ' + CONVERT(VARCHAR(19),@End_Time,120) '

这个输出是:

start_time  conv_s_time
2015-09-17 11:59:10.617 2015-09-17 11:59:10

end_time    conv_e_time
2015-09-18 09:35:10.617 2015-09-18 09:35:10

LogDate ProcessInfo Text
<  2000+ rows of data >

LogDate ProcessInfo Text
<  45,000+ rows of data >

Msg 22004, Level 12, State 1, Line 0
Error executing extended stored procedure: Invalid Parameter Type

对xp_ReadErrorLog的调用使用两个硬编码日期(开始和结束)并使用一个变量作为开始日期,但是当开始日期和结束日期都指定为变量时失败。有没有人有任何关于为什么我不能在调用xp_ReadErrorLog时为这两个参数使用变量的建议?

任何帮助表示感谢。

1 个答案:

答案 0 :(得分:0)

---------------- SQL脚本-----------------------------

Declare @Today varchar(50)
declare @tomorrow varchar(50)
declare @Statement nvarchar(100)

set @today = (select convert(varchar(8), getdate(), 112))
Print @Today
set @Tomorrow= (select CONVERT(VARCHAR(10), GETDATE()-1, 112))
Print @tomorrow

Set @Statement = 'EXEC xp_readerrorlog 0,2, N''error'', N''Failed'', '+'N'''++@tomorrow+''''++','+ ++'N'''+@today+''''
Print @Statement 
exec sp_executesql @Statement

-----------------结果---------------------------

Result sample