创建一个try并捕获内部循环

时间:2014-02-14 16:51:13

标签: sql

我想通过“RPT_ID = 245”从1到245循环并在try中捕获无效的查询语句,以便捕获无效的查询。

查询列是为报告创建的动态sql语句。

alter PROCEDURE usp_ExampleProc

AS
       declare 
    @vsql2        as nvarchar(max)
    ,@vquery2    as nvarchar(max)
    ,@value      as nvarchar(max)
    ,@value2     as nvarchar(max)
    ,@Q1     as nvarchar(max)
    ,@RP_NUM     as nvarchar(max)
    ,@count      as int

    select @count = count(*) from saved_reports

 while   @count > 0

     begin
       select  @q1 =  query,@RP_NUM =RPT_ID from SAVED_REPORTS WHERE RPT_ID = @count
       exec sys.sp_executesql @q1



BEGIN TRY
    EXECUTE usp_ExampleProc;
END TRY 
BEGIN CATCH
    SELECT 
         ERROR_NUMBER() AS ErrorNumber
        ,ERROR_MESSAGE() AS ErrorMessage;
END CATCH
      set @count = @count - 1

     end

1 个答案:

答案 0 :(得分:1)

使用

执行usp_GetErrorInfo;

在你的捕获声明中。

请参阅http://technet.microsoft.com/en-us/library/ms175976.aspx