@@ROWCOUNT
Stores the number of rows affected by the last command. I find myself using this variable all the time for logging. For example, if I have a stored procedure inserting/updating/deleting data from a table, I use this variable to store the results of each of those commands in a log table. This makes debugging much easier and any spikes or drops in records can be captured and easily viewed. A word of warning though - this variable only stores the rowcount for the last command. If you find that you are not storing the correct number, it is likely you have another line of code executing before you are reading this variable.
@@ERROR
Stores the error code for the immediately previous command. I use this most often in conjunction with the @@ROWCOUNT variable, and store them together for logging/debugging purposes. Note that if you want to log both of them for the same line of code, you need to make sure to write them both to the output in a single command. If you don't do this, then whichever variable's value is written second, will contain the result from the writing of the first variable, and not the actual insert/update/delete that is of concern.
@@SPID
Stores the session ID. Again, my advice would be to log it with the previous two values. If you have a number of sessions performing a range of activities, the log table will be confusing without being able to identify which sessions are doing what.
@@TRANCOUNT
Stores the number of currently open transactions. I often perform a check using this variable in my CATCH block to determine if we have hit an error inside a transaction and left it open : IF (@@TRANCOUNT > 0) ROLLBACK
@@CPU_BUSY,
@@IDLE,
@@IO_BUSY
These ones are useful for gathering stats on the SQL box. They store the number of "ticks" that the CPU has been busy doing SQL Server activities, the amount of ticks SQL Server has been idle and the number of ticks SQL Server has spent doing IO. All are measured since the last time SQL Server was started.
@@TIMETICKS
The number of microseconds per tick - this helps to transform the previous stats into understandable time :)
There are many other global variables available in SQL Server, but this is simply meant to be a list of the ones I find myself using most often.
No comments:
Post a Comment