简体   繁体   中英

T-SQL inserting query which consist error into another table

I made a program to perform insert/delete on a table(main table)...My program also write a log for the operations (insert/delete) performed on this this table in another table(audit log table)... The program write the log for every successful or failed operation performed on main table in audit log table...I have one field in my audit log table in which i want to move the reason of failure of the operation, if the operation will fail for main table..

I know the reason for sqlcodes like -503 , +100 , -803 ...But i want to insert reason for every SQL code possible...

Is there any variable in T-SQL , from where i can insert the sql error text message into the table..?? Or there is any other way to do it ?

Try something like this:

BEGIN TRY
    -- Do your work on main table
END TRY
BEGIN CATCH

    INSERT INTO LOG_TABLE
    SELECT ERROR_NUMBER(),
           ERROR_MESSAGE()
END CATCH

This assumes the LOG_TABLE has only two columns, the first column for the error number, the second column for the error message. You can use these functions:

ERROR_LINE (Transact-SQL)
ERROR_NUMBER (Transact-SQL)
ERROR_PROCEDURE (Transact-SQL)
ERROR_SEVERITY (Transact-SQL)
ERROR_STATE (Transact-SQL) 

Yes you can do this, with some complexities and limitations. This is not a recommendation, just an answer.

SQLServer provides built in functions such as ERROR_MESSAGE(), ERROR_NUMBER() for use in a catch block.

If your insert/delete code runs in autocomitting mode (no begin tran, no implicit transactions) then simply recording the error message and number in a catch block should work, because the inserts and deletes are already rolled back or committed.

So

begin try
    -- do work
end try
begin catch
    insert into audit_table select ERROR_MESSAGE(), ERROR_NUMBER()
end catch

can work.

If your TSQL code, or the client, uses begin tran, or implicit transactions, the logic in a catch block must consider this, or the error recording will be rolled back when the original transaction is rolled back, leaving no record of the error.

If you have something like this...

begin try
    begin tran
    -- do work
    if xact_state() = 1 commit tran
    if xact_state() = -1 rollback tran
end try
begin catch
    -- Now there is a good chance a transaction is still pending or uncomittable.
    if xact_state() != 0 rollback tran -- assuming you always rollback.
    -- Insert will be recorded because previous transaction was cleared.
    insert into audit_table select ERROR_MESSAGE(), ERROR_NUMBER()
end catch

If you don't commit or rollback in the catch block, and your transaction is uncomittable, your insert is eventually rolled back, and you have no record of the error.

If the client is managing transactions it may not be possible to record error information unless the client has no problem with your error handler issuing a rollback. And issuing a rollback may itself generate an error that masks the real root cause. Without dwelling on complexities, if the client is managing transactions it can be much easier to let the client catch and record errors too.

--check if error is there then----

  BEGIN  
         RAISERROR('something is wrong',16,2)  
         RETURN -20008  ---specify
    END

Handle it based on returnd number

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM