T-SQL可重用错误捕获代码

时间:2017-06-15 18:56:09

标签: tsql

我有这个错误捕获代码将错误写入日志表。我必须在开始尝试之后重复它,结束尝试。我不想使用环回服务器。如果48个表中的任何一个在插入或更新时出错,我使用带有回滚事务的存储过程。它使我的代码2000行。有没有办法让它成为一个类,就像在其他编程中一样,所以我不必多次在我的脚本中重新粘贴相同的代码?

sql server 2008r

            insert into powercampustest.dbo.ADDRESSSCHEDULE (
            zip_code, 
            address_line_1, 
            address_line_2, 
            address_line_3, 
            city, 
            state, 
            county, 
            alternate_fax, 
            email_address, 
            main_fax, 
            address_type, 
            --SEQUENCE_NO, 
            CREATE_OPID, 
            CREATE_TERMINAL, 
            REVISION_OPID, 
            REVISION_TERMINAL, 
            PEOPLE_ORG_CODE, 
            PEOPLE_ORG_ID, 
            PEOPLE_ORG_CODE_ID, 
            NO_MAIL, 
            STATUS, 
            RECURRING, 
            APPROVED,
            CREATE_DATE,  
            CREATE_TIME,
            REVISION_DATE,
            REVISION_TIME)
            SELECT 
            ZIP, 
            ADDRESS1, 
            ADDRESS2, 
            ADDRESS3, 
            CITY, 
            STATE, 
            (SELECT top 1 CC.CODE_VALUE from [powercampustest].[dbo].[CODE_COUNTY] CC, [Bridge_test].[dbo].[PEOPLECHANGES] UC where CC.[LONG_DESC] like '%' + UC.[UCOUNTY] + '%'), 
            FAX, 
            UEMAILSCT, 
            FAX, 
            --COALESCE(UADDRTYPE, N' '), 
            'PERM', 
            --COALESCE(SEQUENCE_NO, 1), --sequence_no, is identity, it should increment
            COALESCE(CREATE_OPID, N' '), 
            COALESCE(CREATE_TERMINAL, N' '), 
            COALESCE(REVISION_OPID, N' '), 
            COALESCE(REVISION_TERMINAL, N' '),
            COALESCE(PEOPLE_CODE, N' '), 
            COALESCE(PEOPLE_ID, N' '), 
            COALESCE(PEOPLE_CODE_ID, N' '), 
            COALESCE(N' ', N' '), 
            COALESCE(N' ', N' '), 
            COALESCE(N' ', N' '), 
            COALESCE(N' ', N' '),
            COALESCE(CREATE_DATE, GetDate()),  
            COALESCE(CREATE_TIME, GetDate()),  
            COALESCE(REVISION_DATE, GetDate()), 
            COALESCE(REVISION_TIME, GetDate())  
            from Bridge_test.dbo.peoplechanges
            WHERE        (Processed is Null)
        END TRY 
        BEGIN CATCH 
            SET @flag = 'true'
            INSERT INTO @LogErrorsTable
            (
                [Error_Number],
                [Error_Message],
                [Error_Severity],
                [Error_State],
                [Error_Procedure],
                [Error_Line],
                [UserName],
                [HostName],
                [Time_Stamp]
            )
             SELECT ISNULL(ERROR_NUMBER(),0),
                    ISNULL(ERROR_MESSAGE(),'NULL Message'),
                    ISNULL(ERROR_SEVERITY(),0), 
                    ISNULL(ERROR_STATE(),1),    
                    ISNULL(ERROR_PROCEDURE(),''),            
                    ISNULL(ERROR_LINE(), 0), 
                    SUSER_SNAME(), 
                    HOST_NAME(), 
                    GETDATE()           
        END CATCH

0 个答案:

没有答案