开发者

How to view errors live while debugging a script or stored proc in SQL Server Express 2008

New to the SQL Server Express 2008 debugger.

Here's what happens. Let's say I have stored proc with 4 delete statements and the 2nd delete results in a FK violation on some other table. When I exec the sp and flip to the messages tab (which is next to the results tab) I can see the fk violation error in red color.

However if I step into the sp and go line by line there is no live error display. Basically there is no messages tab at all when you step into a sp. I see the call stack window, watch window, breakpoints window etc. I don't have any try catch or the classic if (@@errror) goto there type lines and putting error handling will probably be detected correctly in debugger as well.

I plan to do error handling but for now my question pertain开发者_如何学Pythons to the use of the debugger. As soon as I stop the debugger the messages tab appears and I can see the fk violations. How do I see my errors live instead of having to wait to stop the debugger ? thank you


I would put in the try catch statements and log the error messages to another table with an auto incrimenting sequence number. Then you can select from that to see which error messages and what order they came.

Example:

CREATE TABLE trackedErrors (
      sequence int not null identity(1,1) primary key
    , message varchar(max)
)
GO

BEGIN TRY
    --dangerous sql
    SELECT 5/0
END TRY
BEGIN CATCH
    --log error
    INSERT INTO trackedErrors(message)
    VALUES (ERROR_MESSAGE())
END CATCH

You can do a lot more with error information besides ERROR_MESSAGE(). See the TRY-CATCH documentation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜