调用存储过程时的错误报告

时间:2017-06-12 13:02:19

标签: express sql-server-2008-r2

使用Microsoft SQL Server 2008 R2 Express

我已经设置了一个从网页调用的存储过程。此存储过程在表中执行INSERT。 当被调用的存储过程在服务器端失败时,是否有可能获得错误报告。

提前致谢

叫做SP

USE [TEST]
GO
/****** Object:  StoredProcedure [dbo].[Web_form]    Script Date: 06/08/2017 12:20:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      R H B
-- Create date: 
-- Description: Web form
-- =============================================
ALTER PROCEDURE [dbo].[Web_form] 
    -- Add the parameters for the stored procedure here

    @TitleID INT,                       -- WEB 1 to 3 Mr. Ms. Mrs.
    @Forename nvarchar(30)= NULL,       -- WEB
    @Surname  nvarchar(30)= NULL,       -- WEB
    @DateOfBirth nvarchar(30)= NULL,    -- WEB yyyy-mm-dd
    @hHomeEmail nvarchar(30)= NULL,     -- WEB
    @HomeMobile nvarchar(30)= NULL,     -- WEB
    @Sex nvarchar(5)= NULL,             -- WEB M/F
    @HouseNum nvarchar(5)= NULL,        -- WEB
    @Address1 nvarchar(30)= NULL,       -- WEB @HouseNum + @Address1
    @Address2 nvarchar(30)= NULL,       -- WEB
    @Address3 nvarchar(30)= NULL,       -- WEB
    @Address4 nvarchar(30)= NULL,       -- WEB
    @Address5 nvarchar(20)= NULL,       -- WEB Eircode
    @Notes nvarchar(50)= NULL,          -- WEB Comments
    @pPassword nvarchar(20)= NULL,      -- WEB Min 8 char upper lower digit (generate random temp pw)
    @AffiliationStatusID INT,           -- WEB 1=Full 2=OAP 3=Student
    @ConstituencyPrefix nvarchar(20) = NULL,    -- NO defaut (New Online)
    @BranchID INT = 6,              -- 2246 = New Online (Test) - 2247 = New Online (Test)
    @MemberStatusID INT = 6,            -- 1 to 6 Affiliated,Lapsed,Deceased,Withdrawn,Not Affiliated, Incomplete
    @VoteStatusID INT = 2,              -- 1=Voting 2=Non Voting
    @AffiliationDate nvarchar(20)= NULL,            -- GETDATE()
    @DateFirstJoined nvarchar(20)= NULL             -- GETDATE()
    -- @LastUpdate INT                  -- Not in use
AS

BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    declare @MemberNo INT 

 SELECT @MemberNo = MAX(MemberNo)+1 From dbo.member;



 INSERT dbo.member (
        TitleID, 
        Forename, 
        Surname, 
        DateOfBirth, 
        HomeEmail, 
        HomeMobile, 
        Sex, 
        Address1, 
        Address2, 
        Address3, 
        Address4, 
        Address5,
        Notes, 
        AffiliationStatusID,
        ConstituencyPrefix, 
        MemberNo, 
        BranchID,  
        MemberStatusID, 
        VoteStatusID, 
        AffiliationDate, 
        DateFirstJoined
        ) 

    VALUES (
        @TitleID, 
        @Forename, 
        @Surname, 
        @DateOfBirth, 
        @hHomeEmail, 
        @HomeMobile, 
        'U',
        @HouseNum + @Address1, 
        @Address2, 
        @Address3, 
        @Address4, 
        @Address5,
        --CASE @Address5
        --  WHEN '' THEN 'None'
        --  ELSE @Address5
        --  END,
        @Notes,
        @AffiliationStatusID,
        'NO', 
        @MemberNo, 
        2247,  
        1, 
        2, 
        GETDATE(), 
        GETDATE() 
        );  


 SELECT MemberID, 
        ConstituencyPrefix, 
        MemberNo, 
        BranchID, 
        Surname, 
        Forename, 
        TitleID, 
        Address1, 
        Address2, 
        Address3,
        Address4,
        Address5,
        HomeEmail,
        HomeMobile,
        AffiliationDate FROM Member WHERE MemberNo = @MemberNo ;


END

1 个答案:

答案 0 :(得分:0)

您可以使用Try catch。创建错误表以捕获错误数据。在catch块中写入一个insert语句来捕获错误。然后,您可以从此表创建报告。

DECLARE @inputParams VARCHAR(max)

SELECT  @inputParams = 'your input variables'

    BEGIN TRY
        BEGIN Transaction

        DO Something

    END TRY

    BEGIN CATCH
    IF @@TRANCOUNT > 0
        ROLLBACK Transaction;--ROLLBACK IN CASE OF ERROR

    INSERT INTO ErrorLog
    VALUES (
        ERROR_NUMBER()
        ,ERROR_SEVERITY()
        ,ERROR_STATE()
        ,ERROR_PROCEDURE()
        ,ERROR_LINE()
        ,ERROR_MESSAGE()
        ,GETDATE()
        ,@inputParams
        );

    THROW;
END CATCH

IF @@TRANCOUNT > 0
    COMMIT Transaction;