2016-08-24 50 views
1

sql server 2012

我有一個過程來包裝sp_help_jobhistory的輸出。存儲過程執行該執行的時候沒有問題,但返回錯誤:sp_help_jobhistory WITH RESULT SETS錯誤

SET FMTONLY OFF 
EXEC msdb.dbo.sp_describe_first_result_set @tsql = N'exec msdb.dbo.sp_help_jobhistory_with_results' 
GO 

這是我得到的錯誤:

Msg 11512, Level 16, State 1, Procedure sp_describe_first_result_set, Line 1 [Batch Start Line 3] 
The metadata could not be determined because the statement 'EXEC msdb.dbo.sp_help_jobhistory 
     @job_id 
     ,@job_name 
     ,@step_id 
     ,@sql_message_id 
     ,@sql' in procedure 'sp_help_jobhistory_with_results' is not compatible with the statement 'EXEC msdb.dbo.sp_help_jobhistory 
     @job_id 
     ,@job_name 
     ,@step_id 
     ,@sql_message_id 
     ,@sql' in procedure 'sp_help_jobhistory_with_results'. 

我在做什麼錯? 下面是代碼

use msdb 
go 


create proc [dbo].[sp_help_jobhistory_with_results] 
@job_id     uniqueidentifier=null 
,@job_name    sysname=null 
,@step_id    int=null 
,@sql_message_id  int=null 
,@sql_severity   int=null 
,@start_run_date  int=null 
,@end_run_date   int=null 
,@start_run_time  int=null 
,@end_run_time   int=null 
,@minimum_run_duration int=null 
,@run_status   int=null 
,@minimum_retries  int=null 
,@oldest_first   int=0 
,@server    nvarchar(30)=null 
,@mode     varchar(7)='SUMMARY' 
AS 
BEGIN 
    IF (@mode <>'SUMMARY' and (@job_name is not null or @step_id is not null)) 
    BEGIN 
     -- returns 17 columns 
     EXEC msdb.dbo.sp_help_jobhistory 
     @job_id 
     ,@job_name 
     ,@step_id 
     ,@sql_message_id 
     ,@sql_severity 
     ,@start_run_date 
     ,@end_run_date 
     ,@start_run_time 
     ,@end_run_time 
     ,@minimum_run_duration 
     ,@run_status 
     ,@minimum_retries 
     ,@oldest_first 
     ,@server 
     ,@mode 
     WITH RESULT SETS 
     ( 
      (
      instance_id   int 
      ,job_id    uniqueidentifier 
      ,job_name   sysname 
      ,step_id   int 
      ,step_name   sysname 
      ,sql_message_id  int 
      ,sql_severity  int 
      ,[message]   nvarchar(1024) 
      ,run_status   int 
      ,run_date   int 
      ,run_time   int 
      ,run_duration  int 
      ,operator_emailed nvarchar(20) 
      ,operator_netsent nvarchar(20) 
      ,operator_paged  nvarchar(20) 
      ,retries_attempted int 
      ,[server]   nvarchar(30) 
      ) 
     ) 
    END 
    -- returns 11 columns 
    ELSE 
    BEGIN 
     EXEC msdb.dbo.sp_help_jobhistory 
     @job_id 
     ,@job_name 
     ,@step_id 
     ,@sql_message_id 
     ,@sql_severity 
     ,@start_run_date 
     ,@end_run_date 
     ,@start_run_time 
     ,@end_run_time 
     ,@minimum_run_duration 
     ,@run_status 
     ,@minimum_retries 
     ,@oldest_first 
     ,@server 
     ,@mode 
     WITH RESULT SETS 
     ( 
      (
      job_id    uniqueidentifier 
      ,job_name   sysname 
      ,run_status   int 
      ,run_date   int 
      ,run_time   int 
      ,run_duration  int 
      ,operator_emailed nvarchar(20) 
      ,operator_netsent nvarchar(20) 
      ,operator_paged  nvarchar(20) 
      ,retries_attempted int 
      ,[server]   nvarchar(30) 
      ) 
     ) 
    END 
END 

回答

1

sp_help_jobhistory_with_results不能確定METADATA不使用WITH RESULT SETS,就像你在你的主要程序一樣。使用此替代(你必須填寫列)

EXEC msdb.dbo.sp_describe_first_result_set @tsql = N'exec msdb.dbo.sp_help_jobhistory_with_results with result sets ((Col1 datatype, Col2 datatype, etc....))' 
+0

我跟着這裏的示例[https://blogs.msdn.microsoft.com/sqlagent/2012/07/12/workaround-sql-server-2012-openrowset-on-sp_help_job-throws-the-metadata-能而不是待確定/(https://blogs.msdn.microsoft.com/sqlagent/2012/07/12/workaround-sql-server-2012-openrowset-on-sp_help_job-throws-the-metadata-無法確定/) 它不需要指定'WITH RESULT SETS' –

+0

確定@ToDo但它沒有正常工作?您是否嘗試過使用結果集? – scsimon

+0

上述鏈接中的示例沒有指定'WITH RESULT SETS'。 您的建議適用於我的。我不知道我和他有什麼不同。 –

0

系統存儲過程sp_describe_first_result_setRaises an error if the Database Engine cannot determine the metadata for the first query that will be executed by performing a static analysis.https://msdn.microsoft.com/en-us/library/ff878602.aspx

我以前沒有使用這個存儲過程,所以我不知道爲什麼你需要運行它。我沒有看到任何證據表明它是針對存儲過程運行的。 http://stevestedman.com/2013/04/t-sql-2012-procedure-sp_describe_first_result_set/

+0

我按照這裏的例子[https://blogs.msdn.microsoft.com/sqlagent/2012/07/12/workaround-sql-server-2012-openrowset-on-sp_help_job-throws-the-metadata-could-not -BE確定/(https://blogs.msdn.microsoft.com/sqlagent/2012/07/12/workaround-sql-server-2012-openrowset-on-sp_help_job-throws-the-metadata-could-not -be-determined /) sp_describe_first_result_set返回Transact-SQL批處理的第一個可能的結果集的元數據。這就是我使用它的原因。 –

相關問題