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.

Chapter 11: Writing Scripts and Batches<br />

330<br />

Variable Purpose Comments<br />

@@REMSERVER Used only in stored<br />

procedures. Returns<br />

the value of the<br />

server that called the<br />

stored procedure.<br />

@@ROWCOUNT One of the most<br />

used system functions.<br />

Returns the<br />

number of rows<br />

affected by the last<br />

statement.<br />

SCOPE_IDENTITY Similar to @@IDEN-<br />

TITY, but returns the<br />

last identity inserted<br />

within the current<br />

session and scope.<br />

@@SERVERNAME Returns the name<br />

of the local server<br />

that the script is<br />

running from.<br />

@@TRANCOUNT Returns the number<br />

of active transactions<br />

— essentially<br />

the transaction nesting<br />

level — for the<br />

current connection.<br />

@@VERSION Returns the current<br />

version of <strong>SQL</strong><br />

<strong>Server</strong> as well as the<br />

date, processor, and<br />

O/S architecture.<br />

Handy when you want the sproc to behave differently<br />

depending on the remote server (often a<br />

geographic location) from which it was called.<br />

Still, in this era of .NET, I would question<br />

whether anything needing this variable might<br />

have been better written using other functionality<br />

found in .NET.<br />

Commonly used in non runtime error checking.<br />

For example, if you try to DELETE a row using a<br />

WHERE clause, and no rows are affected, then that<br />

would imply that something unexpected happened.<br />

You can then raise an error manually.<br />

Very useful for avoiding issues where a trigger or<br />

nested stored procedure has performed additional<br />

inserts that have overwritten your<br />

expected identity value.<br />

Can be changed by using sp_addserver and<br />

then restarting <strong>SQL</strong> <strong>Server</strong>, but rarely required.<br />

A ROLLBACK TRAN statement decrements<br />

@@TRANCOUNT to 0 unless you are using save<br />

points. BEGIN TRAN increments @@TRANCOUNT by<br />

1, COMMIT TRAN decrements @@TRANCOUNT by 1.<br />

Unfortunately, this doesn’t return the information<br />

into any kind of structured field arrangement, so<br />

you have to parse it if you want to use it to test<br />

for specific information. Also be sure to check out<br />

the xp_msver extended stored procedure.<br />

Don’t worry if you don’t recognize some of the terms in a few of these. They will become clear in due<br />

time, and you will have this table or Appendix A to look back on for reference at a later date. The thing<br />

to remember is that there are sources you can go to in order to find out a whole host of information<br />

about the current state of your system and your activities.

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

Saved successfully!

Ooh no, something went wrong!