原来SQL也有try-catch
今天看项目示例代码,存储过程里也有写try-catch代码块,觉得很新鲜,不知道是不是SQL 2005的新语法(以前一直用2000),代码如下
CREATE PROC [dbo].[p_TNoticeAdmin_RowByLastModifyDate]
@intNoticeAdminID INT
AS
SET NOCOUNT ON;
-- �������� Declare
DECLARE @btResult BIT; --������ ���� ���� (0: ����, 1: ����)
-- MAIN CODE: Begin
BEGIN TRY
SELECT ModifyDate FROM TNoticeAdmin
WHERE NoticeAdminID = @intNoticeAdminID
SET @btResult = 0;
END TRY
BEGIN CATCH
DECLARE @strErrContent VARCHAR(50),
@intErrorNumber INT,
@intErrorSeverity INT,
@intErrorState INT,
@strErrorProcedure NVARCHAR(126),
@intErrorLine INT,
@strErrorMessage NVARCHAR(4000);
SET @strErrContent = '�������� ȯ�������� ���������� ���� �б� ó�� ����';
SET @intErrorNumber = ERROR_NUMBER();
SET @intErrorSeverity = ERROR_SEVERITY();
SET @intErrorState = ERROR_STATE();
SET @strErrorProcedure = ERROR_PROCEDURE();
SET @intErrorLine = ERROR_LINE();
SET @strErrorMessage = ERROR_MESSAGE();
EXEC p_TDBErrLog_Create @strErrContent, @intErrorNumber, @intErrorSeverity, @intErrorState,
@strErrorProcedure, @intErrorLine, @strErrorMessage;
RAISERROR(@strErrorMessage, @intErrorSeverity, @intErrorState, @strErrorProcedure, @intErrorLine);
SET @btResult = 1;
END CATCH
@intNoticeAdminID INT
AS
SET NOCOUNT ON;
-- �������� Declare
DECLARE @btResult BIT; --������ ���� ���� (0: ����, 1: ����)
-- MAIN CODE: Begin
BEGIN TRY
SELECT ModifyDate FROM TNoticeAdmin
WHERE NoticeAdminID = @intNoticeAdminID
SET @btResult = 0;
END TRY
BEGIN CATCH
DECLARE @strErrContent VARCHAR(50),
@intErrorNumber INT,
@intErrorSeverity INT,
@intErrorState INT,
@strErrorProcedure NVARCHAR(126),
@intErrorLine INT,
@strErrorMessage NVARCHAR(4000);
SET @strErrContent = '�������� ȯ�������� ���������� ���� �б� ó�� ����';
SET @intErrorNumber = ERROR_NUMBER();
SET @intErrorSeverity = ERROR_SEVERITY();
SET @intErrorState = ERROR_STATE();
SET @strErrorProcedure = ERROR_PROCEDURE();
SET @intErrorLine = ERROR_LINE();
SET @strErrorMessage = ERROR_MESSAGE();
EXEC p_TDBErrLog_Create @strErrContent, @intErrorNumber, @intErrorSeverity, @intErrorState,
@strErrorProcedure, @intErrorLine, @strErrorMessage;
RAISERROR(@strErrorMessage, @intErrorSeverity, @intErrorState, @strErrorProcedure, @intErrorLine);
SET @btResult = 1;
END CATCH
呵呵,虽然SQL 2008都要出来了,做了次小白,但这也是一大收获。