locked
Detailed error log framework in sql server RRS feed

  • Question

  • User-873890331 posted

    Hello,

    I want to save detailed error information which could occur on any sql statement. For example if I am using following sql statement in a procedure 

    Insert into <TableName>
    select * from <TableName>

    if this query throw error then I want to save detailed information into table like " exception occur on following data: Id = 1, TypeId cannot be NULL"

    Apart bulk Insert statement I want to log If exception occur in a function then error should be save into table.

    Can any one suggest me how to implement it.

    Thanks & Regards
    Shwetamber   

    Tuesday, July 5, 2016 11:21 AM

Answers

  • User61956409 posted

    Hi Shwetamber,

    if this query throw error then I want to save detailed information into table like " exception occur on following data: Id = 1, TypeId cannot be NULL"

    You could try to implement error handling for Transact-SQL using TRY...CATCH.

    BEGIN TRY  
        -- put your query here.  
        Insert into [TableName];  
    END TRY  
    BEGIN CATCH  
        -- insert error message to table.  
    	INSERT INTO [tablename] ([ColumnName]) VALUES ((SELECT ERROR_MESSAGE() AS ErrorMessage));  
    END CATCH;
    

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 13, 2016 10:33 AM

All replies

  • User379720387 posted

    Elmah is what you are looking for.

    Tuesday, July 5, 2016 2:05 PM
  • User61956409 posted

    Hi Shwetamber,

    if this query throw error then I want to save detailed information into table like " exception occur on following data: Id = 1, TypeId cannot be NULL"

    You could try to implement error handling for Transact-SQL using TRY...CATCH.

    BEGIN TRY  
        -- put your query here.  
        Insert into [TableName];  
    END TRY  
    BEGIN CATCH  
        -- insert error message to table.  
    	INSERT INTO [tablename] ([ColumnName]) VALUES ((SELECT ERROR_MESSAGE() AS ErrorMessage));  
    END CATCH;
    

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 13, 2016 10:33 AM