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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Appendix A: System Functions<br />

In addition, we have the OVER operator, which, while largely working as a ranking tool, can be applied<br />

to other forms of T-<strong>SQL</strong> functions (most notably aggregates). While I only discuss it as part of the ranking<br />

functions, you may see it referenced several other places in this appendix.<br />

Legacy System Functions<br />

(a.k.a. Global Variables)<br />

@@CONNECTIONS<br />

Returns the number of connections attempted since the last time your <strong>SQL</strong> <strong>Server</strong> was started.<br />

This one is the total of all connection attempts made since the last time your <strong>SQL</strong> <strong>Server</strong> was started. The<br />

key thing to remember here is that we are talking about attempts, not actual connections, and that we<br />

are talking about connections as opposed to users.<br />

Every attempt made to create a connection increments this counter regardless of whether that connection<br />

was successful or not. The only catch with this is that the connection attempt has to have made it as far<br />

as the server. If the connection failed because of NetLib differences or some other network issue, then<br />

your <strong>SQL</strong> <strong>Server</strong> wouldn’t even know that it needed to increase the count — it only counts if the server<br />

saw the connection attempt. Whether the attempt succeeded or failed does not matter.<br />

It’s also important to understand that we’re talking about connections instead of login attempts. Depending<br />

on your application, you may create several connections to your server, but you’ll probably only ask<br />

the user for information once. Indeed, even Query Analyzer does this. When you click for a new window,<br />

it automatically creates another connection based on the same login information.<br />

This, like a number of other system functions, is often better served by a system stored procedure,<br />

sp_monitor. This procedure, in one command, produces the information from the number of connections,<br />

CPU busy, through to the total number of writes by <strong>SQL</strong> <strong>Server</strong>. So, if basic information is what<br />

you’re after, then sp_monitor may be better — if you need discrete data that you can manipulate,<br />

then @@CONNECTIONS provides a nice, neat, scalar piece of data.<br />

@@CPU_BUSY<br />

588<br />

Returns the time in milliseconds that the CPU has been actively doing work since <strong>SQL</strong> <strong>Server</strong> was last<br />

started. This number is based on the resolution of the system timer — which can vary — and can therefore<br />

vary in accuracy.<br />

This is another of the “since the server started” kind of functions. This means that you can’t always count<br />

on the number going up as your application runs. It’s possible, based on this number, to figure out a CPU<br />

percentage that your <strong>SQL</strong> <strong>Server</strong> is taking up. Realistically though, I’d rather tap right into the Performance<br />

Monitor for that if I had some dire need for it. The bottom line is that this is one of those really cool<br />

things from a “gee, isn’t it swell to know that” point of view, but doesn’t have all that many practical<br />

uses in most applications.

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

Saved successfully!

Ooh no, something went wrong!