从存储过程捕获错误消息

时间:2016-09-23 09:50:06

标签: sql stored-procedures sql-server-2012

如何捕获错误消息等"将数据类型varchar转换为datetime"

时出错

并将此错误放入选择输出

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.min.css" />
<style>
.bdr {
	position: absolute;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	margin: auto;
	width: 15px;
	height: 2px;
	background-color: #000;
}
 @media only screen and (max-width: 550px) {
.mt_10 {
	margin-top: 5px;
	margin-bottom: 5px;
}
.bdr {
	height: 15px;
	width: 2px;
}
.mb_40 {
	margin-bottom: 40px;
}
}
</style>
</head>

<body>
<div class="col-xs-12 col-sm-12">
  <div class=" col-xs-12 col-sm-3 mt_10">
    <p-dropdown [options]="fieldsForDropdown" [(ngModel)]="selectedField" (onChange)="onFieldChanged($event)"></p-dropdown>
  </div>
  <div class="col-xs-12 col-sm-7" style="position:relative; padding:0;">
    <div class="col-xs-12 col-sm-6 mt_10 mb_40">
      <input pInputText class="form-control" type='date' [(ngModel)]="fromVal" (change)="addFROMFilter($event)" />
    </div>
    <div class="bdr"></div>
    <div class="col-xs-12 col-sm-6 mt_10">
      <input pInputText class="form-control" type='date' [(ngModel)]="toVal" (change)="addTOFilter($event)" />
    </div>
  </div>
  <div class="col-xs-12 col-sm-2 mt_10">
    <button class="btn btn-default btn-lg" (click)="applyDateFilter($event)">Apply</button>
  </div>
</div>
</body>
</html>

我不需要==&gt; https://postimg.org/image/u8mohtl9b/

任何解决方案?

3 个答案:

答案 0 :(得分:1)

您可以选择以下错误消息...

Begin Catch     
      SELECT 'Error_Message: ' + ERROR_MESSAGE() + ' Error_Line: ' + ERROR_LINE() AS ErrorMessage    
End Catch 

答案 1 :(得分:1)

您遇到的问题是程序中没有出现错误,调用程序时会发生错误。在一个简单示例中,创建以下过程:

IF OBJECT_ID(N'dbo.ErrorCatchTest', 'P') IS NOT NULL
    DROP PROCEDURE dbo.ErrorCatchTest;
GO
CREATE PROCEDURE dbo.ErrorCatchTest @int INT
AS
BEGIN
    SET NOCOUNT ON;
    BEGIN TRY
        DECLARE @T TABLE (I INT);
        INSERT @T (I) VALUES (5.0 / @int);

        SELECT 'OK';
    END TRY
    BEGIN CATCH
        SELECT CONCAT('Error_Message: ', ERROR_MESSAGE());
    END CATCH
END;

如果我将有效的INT传递给程序:

EXECUTE dbo.ErrorCatchTest @int = 1;

我根据需要获得OK

enter image description here

如果我通过0强制错误:

EXECUTE dbo.ErrorCatchTest @int = 0;

enter image description here

您会根据需要收到错误消息,但如果我尝试传递无效的整数:

EXECUTE dbo.ErrorCatchTest @int = 'Not a number';

enter image description here

然后我收到错误消息,因为错误不在程序中,而是在对参数进行隐式转换时。

解决这个问题的方法是在try / catch块中调用该过程:

BEGIN TRY
    EXECUTE dbo.ErrorCatchTest @int = 'Not a number';
END TRY
BEGIN CATCH
    SELECT CONCAT('Error_Message: ', ERROR_MESSAGE()); 
END CATCH

enter image description here

答案 2 :(得分:0)

如果您只需要错误消息,请使用以下脚本。

BEGIN CATCH
SELECT 'Error_Message :'+ERROR_MESSAGE()
END CATCH

示例输出:

enter image description here