17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Appendix A: System Functions<br />

@@TOTAL_READ and @@TOTAL_WRITE<br />

Respectively return the total number of disk reads/writes by <strong>SQL</strong> <strong>Server</strong> since it was last started.<br />

The names here are a little misleading, as these do not include any reads from cache — they are only<br />

physical I/O.<br />

@@TRANCOUNT<br />

Returns the number of active transactions — essentially the transaction nesting level — for the current<br />

connection.<br />

This is a very big one when you are doing transactioning. I’m not normally a big fan of nested transactions,<br />

but there are times where they are difficult to avoid. As such, it can be important to know just<br />

where you are in the transaction-nesting side of things (for example, you may have logic that only starts<br />

a transaction if you’re not already in one).<br />

If you’re not in a transaction, then @@TRANCOUNT is 0. From there, let’s look at a brief example:<br />

SELECT @@TRANCOUNT As TransactionNestLevel --This will be zero<br />

--at this point<br />

BEGIN TRAN<br />

SELECT @@TRANCOUNT As TransactionNestLevel --This will be one<br />

--at this point<br />

BEGIN TRAN<br />

SELECT @@TRANCOUNT As TransactionNestLevel --This will be two<br />

--at this point<br />

COMMIT TRAN<br />

SELECT @@TRANCOUNT As TransactionNestLevel --This will be back to one<br />

--at this point<br />

ROLLBACK TRAN<br />

SELECT @@TRANCOUNT As TransactionNestLevel --This will be back to zero<br />

--at this point<br />

Note that, in this example, the @@TRANCOUNT at the end would also have reached zero if we had a COM-<br />

MIT as our last statement.<br />

Aggregate Functions<br />

590<br />

Aggregate functions are applied to sets of records rather than a single record. The information in the<br />

multiple records is processed in a particular manner and then is displayed in a single record answer.<br />

Aggregate functions are often used in conjunction with the GROUP BY clause.<br />

The aggregate functions are:<br />

❑ AVG<br />

❑ CHECKSUM

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!