简体   繁体   English

T-SQL将包含错误的查询插入到另一个表中

[英]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... 我知道sqlcodes的原因如-503,+ 100,-803 ......但我想为每个SQL代码插入原因...

Is there any variable in T-SQL , from where i can insert the sql error text message into the table..?? 在T-SQL中是否有任何变量,从那里我可以将sql错误文本消息插入表中。 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. 这假设LOG_TABLE只有两列,第一列是错误号,第二列是错误消息。 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. SQLServer提供内置函数,如ERROR_MESSAGE(),ERROR_NUMBER(),用于catch块。

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. 如果插入/删除代码在自动命令模式下运行(没有begin tran,没有隐式事务),则只需在catch块中记录错误消息和数字即可,因为插入和删除已经回滚或提交。

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. 如果您的TSQL代码或客户端使用begin tran或隐式事务,则catch块中的逻辑必须考虑这一点,否则在回滚原始事务时将回滚错误记录,不会记录错误。

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. 如果您没有在catch块中提交或回滚,并且您的事务是不可注销的,那么您的插入最终会回滚,并且您没有错误记录。

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 根据返回号码处理它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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