You must Sign In to post a response.
  • Category: SQL Server

    MySQL Error handling

    Hello.

    Can you please suggest how to maintain error handling in mysql.
    I want to create 1 table with required columns.
    In store procedures whenever, control goes to catch block, I want to insert error data into table.

    Please give table structure and store procedure.
  • #767674
    You can use "@@ERROR" for doing this. You can check if the "@@ERROR" value not equal to 0. If it is not equal to 0, you can insert the details in to the error table. In the @@ERROR you will get corresponding error number so that you can store the error number also
    Following is the sample code

    DELETE FROM MyTableName
    IF @@ERROR <> 0
    BEGIN
    // Insert the error details into your log table with error number
    END

    By Nathan
    Direction is important than speed

  • #767675
    If you want to use try catch block you can try the following.

    BEGIN TRY
    // Your logical code part
    END TRY
    BEGIN CATCH
    // SELET ERROR_NUMBER() AS ErrorNumber
    // ,ERROR_MESSAGE() AS ErrorMessage;
    // Insert the error message in to you table
    END CATCH;
    GO

    By Nathan
    Direction is important than speed

  • #767686
    Hi,

    You have to track the below details to store the exception details in to database.

    -> LogId
    -> Exception Message
    -> Exception Type
    -> Exception URL
    -> LogDate

    create table using those fields and then create your stored procedure like below by referring below link
    http://www.c-sharpcorner.com/UploadFile/0c1bb2/logging-exception-to-database/

    Hope this helps you...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #767689
    Hai Pranjal,
    You can check when the error bases on the error codes in MySql and then we can insert the records in to the table:
    For e.g., if there is an Sql Exception, we can write the statement:

    DECLARE EXIT HANDLER FOR SQLEXCEPTION
    -- insert the record in to the table

    You can also o through the below link for various types of error declaration and then use the insert statement to insert the records in to the new table which contains all the error related logs:

    http://www.mysqltutorial.org/mysql-error-handling-in-stored-procedures/

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


  • Sign In to post your comments