| Business Application Development with: SQL Server, C#, VB, VB.Net, ASP, ASP.Net, and XML |
| N | S |
Novick Software Management • Design • Programming • Training • Consulting |
|
|
News Links Schedule Site Map Contact |
|
Tips and Tricks for: SQL Server T-SQLThe Problem: Testing @@Error and @@RowCount one after the other.T-SQL's @@ERROR function is reset after every statement and so is @@RowCount. It's so easy to write incorrect T-SQL that tests one of these two functions in one statement and then tests the other one in the next statement. This script illustrates: declare @i int This is what you get: Server: Msg
8134, Level 16, State 1, Line 2 As you can see in the first batch execution of the IF statement reset @@Error. In the second batch where the IF statement was removed, @@Error has the correct error code for the divide by zero. Solution: Save @@Error and @@RowCount after every statement.As the script above shows, doing any thing after a SQL statement resets @@Error. The solution is to save both in a select statement after each significant SQL DML or DDL statement in a procedure. DECLARE @myError int, @myRowCount int -- Do your SQL DML statement or SQL DDL statement here.... SELECT @myError = @@Error, @myRowCount = @@RowCount Since neither function is reset until after the entire SELECT is completed, you can capture both for later use. Yes, this is tedious but in T-SQL it's the only way.
|
|
|
Copyright © 2003-2008 Novick Software, Inc. | Terms of Use | Privacy Policy | Nice Things People Say| |