Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
Appendix A: System Functions @@TOTAL_READ and @@TOTAL_WRITE Respectively return the total number of disk reads/writes by SQL Server since it was last started. The names here are a little misleading, as these do not include any reads from cache — they are only physical I/O. @@TRANCOUNT Returns the number of active transactions — essentially the transaction nesting level — for the current connection. This is a very big one when you are doing transactioning. I’m not normally a big fan of nested transactions, but there are times where they are difficult to avoid. As such, it can be important to know just where you are in the transaction-nesting side of things (for example, you may have logic that only starts a transaction if you’re not already in one). If you’re not in a transaction, then @@TRANCOUNT is 0. From there, let’s look at a brief example: SELECT @@TRANCOUNT As TransactionNestLevel --This will be zero --at this point BEGIN TRAN SELECT @@TRANCOUNT As TransactionNestLevel --This will be one --at this point BEGIN TRAN SELECT @@TRANCOUNT As TransactionNestLevel --This will be two --at this point COMMIT TRAN SELECT @@TRANCOUNT As TransactionNestLevel --This will be back to one --at this point ROLLBACK TRAN SELECT @@TRANCOUNT As TransactionNestLevel --This will be back to zero --at this point Note that, in this example, the @@TRANCOUNT at the end would also have reached zero if we had a COM- MIT as our last statement. Aggregate Functions 590 Aggregate functions are applied to sets of records rather than a single record. The information in the multiple records is processed in a particular manner and then is displayed in a single record answer. Aggregate functions are often used in conjunction with the GROUP BY clause. The aggregate functions are: ❑ AVG ❑ CHECKSUM
❑ CHECKSUM_AGG ❑ COUNT ❑ COUNT_BIG ❑ GROUPING ❑ MAX ❑ MIN ❑ STDEV ❑ STDEVP ❑ SUM ❑ VAR ❑ VARP In most aggregate functions, the ALL or DISTINCT keywords can be used. The ALL argument is the default and will apply the function to all the values in the expression, even if a value appears numerous times. The DISTINCT argument means that a value will only be included in the function once, even if it occurs several times. Aggregate functions cannot be nested. The expression cannot be a subquery. AVG AVG returns the average of the values in expression. The syntax is as follows: AVG([ALL | DISTINCT] ) The expression must contain numeric values. NULL values are ignored. This function supports the OVER operator described in the ranking functions section of this appendix. CHECKSUM This is a basic hash algorithm usually used to detect changes or consistency in data. This particular function accepts either an expression as an argument or a * (which implies that you want all columns in all the joined tables to be included). The basic syntax is: CHECKSUM(, […n] | * ) Note that the order of your expression — or in the case of a *, the join order — will affect the checksum value, so, for example: CHECKSUM(SalesOrderID, OrderDate) would not give the same result as: CHECKSUM(OrderDate, SalesOrderID ) This function is NOT compatible with the OVER operator. Apendix A: System Functions 591
- Page 578 and 579: Chapter 17: Reporting for Duty, Sir
- Page 580 and 581: Chapter 17: Reporting for Duty, Sir
- Page 582 and 583: Chapter 18: Getting Integrated with
- Page 584 and 585: Chapter 18: Getting Integrated with
- Page 586 and 587: Chapter 18: Getting Integrated with
- Page 588 and 589: Chapter 18: Getting Integrated with
- Page 590 and 591: Chapter 18: Getting Integrated with
- Page 592 and 593: Chapter 18: Getting Integrated with
- Page 594 and 595: Chapter 18: Getting Integrated with
- Page 596 and 597: Chapter 18: Getting Integrated with
- Page 598 and 599: Chapter 18: Getting Integrated with
- Page 601 and 602: 19 Playing Administrator And so, he
- Page 603 and 604: ❑ Write the information to the ev
- Page 605 and 606: Creating Jobs and Tasks Using Manag
- Page 607 and 608: Let’s go ahead and move on to the
- Page 609 and 610: Figure 19-8 Figure 19-9 Chapter 19:
- Page 611 and 612: Figure 19-11 Figure 19-12 Chapter 1
- Page 613 and 614: Backup and Reco very No database-dr
- Page 615 and 616: ❑ Differential: This might be ref
- Page 617 and 618: ❑ Simple: Under this model, the t
- Page 619 and 620: The older DBCC commands are still s
- Page 621 and 622: many rows inserted over time and th
- Page 623: Chapter 19: Playing Administrator T
- Page 626 and 627: Appendix A: System Functions In add
- Page 630 and 631: Appendix A: System Functions CHECKS
- Page 632 and 633: Appendix A: System Functions SUM Th
- Page 634 and 635: Appendix A: System Functions The de
- Page 636 and 637: Appendix A: System Functions @@REMS
- Page 638 and 639: Appendix A: System Functions Cert_I
- Page 640 and 641: Appendix A: System Functions Encryp
- Page 642 and 643: Appendix A: System Functions @@FETC
- Page 644 and 645: Appendix A: System Functions CURREN
- Page 646 and 647: Appendix A: System Functions SYSDAT
- Page 648 and 649: Appendix A: System Functions GetRoo
- Page 650 and 651: Appendix A: System Functions The ex
- Page 652 and 653: Appendix A: System Functions RAND T
- Page 654 and 655: Appendix A: System Functions ❑ IN
- Page 656 and 657: Appendix A: System Functions ❑ Is
- Page 658 and 659: Appendix A: System Functions FILEGR
- Page 660 and 661: Appendix A: System Functions INDEX_
- Page 662 and 663: IsCheckCnst IsConstraint IsDefault
- Page 664 and 665: Appendix A: System Functions OBJECT
- Page 666 and 667: Appendix A: System Functions TYPEPR
- Page 668 and 669: Appendix A: System Functions The qu
- Page 670 and 671: Appendix A: System Functions The SU
- Page 672 and 673: Appendix A: System Functions ASCII
- Page 674 and 675: Appendix A: System Functions The ch
- Page 676 and 677: Appendix A: System Functions UNICOD
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