PDA

View Full Version : What's with the "@" symbol?



malik641
01-07-2008, 09:07 AM
In SQL Server:

I don't know where to find any documentation on what the "@" symbol means and why it is used. I've done different kinds of advanced google searches for this (e.g. "inurl:forum "@.symbol") and I get squat. I've seen it used plenty of times, but nobody explains it...

Can anybody explain what this is and what it does and how to use it? Or maybe a link to a site that explains it?

matthewspatrick
01-07-2008, 06:12 PM
Joseph,

Can you explain the context? Generally, user variable names in a SQL batch are preceded by an @:



DECLARE @MyVariable varchar(100)
SET @MyVariable = 'Joseph is pretty cool!'


Certain system state values are preceded by @@. For example, to see what is currently set as the first weekday of the week:



SELECT @@DATEFIRST AS FirstDayOfWeek


If that's not what you mean, then... :dunno

malik641
01-07-2008, 06:36 PM
Patrick,

First, thank you for the response. It has given some insight towards finding my answer.

And I'm sorry, but I truly didn't know what context it held! The only thing I could think of was the variable thing (since I've seen it that way one time), but I didn't even know if that was right.

I have also seen it without being declared; like in the middle of the query:

SELECT col1 FROM @Table1 WHERE ...

Or something the like. This made me think that the sql engine would prompt the user for some kind of input (like an input box saying "What table do you want to grab data from?").

The most frustrating part is that I can't seem to do a search for this symbol in any context with SQL. And also, I'm not sure if this "@" character is part of T-SQL's extended functionality, or considered just SQL. Regardless, I've tried searching for both and had no luck.

And for some insane reason, I thought that maybe it's some special character for searches...like a wildcard or something. Man, I even tried "\@" to maybe escape that character!


I guess I just don't like the fact that I can't find information on this "secretly special" '@' symbol anywhere.

Thanks again for the input Patrick :) If you have any links to more queries like that, I'm well interested!

matthewspatrick
01-07-2008, 06:41 PM
Joseph,

Based on the syntax you posted above, I would guess that @Table1 is a table variable: T-SQL allows you to define variables of data type table. These table variables generally behave the same way "real" tables behave. The big exception, of course, is that they exist only in RAM, and once the SQL code stops executing, they evaporate.

I rarely do this; typically, whenever a table variable would useful, I will just create a temporary table instead. I'm not saying my way is better--it's just how I learned how to do things, and I have not yet seen any good reason to break the habit.

malik641
01-07-2008, 07:14 PM
Patrick,

Ah. I see. That's an interesting thought - to use RAM for a temporary table. I would think you would want to keep the data a little longer than when the query stopped. Unless that query ended up giving back some kind of calculated answer for ya.

So I think I'll look towards T-SQL for some more info on this. That will narrow down my search pretty well.

Thanks again Patrick. If I find some interesting links about this, I'll post them here.

stanl
01-10-2008, 06:26 AM
you might find this helpful

http://msdn2.microsoft.com/en-us/library/ms187342.aspx

Stan

malik641
01-16-2008, 12:49 PM
Thanks Stan, I'll take a look into it.

XLGibbs
03-04-2008, 07:47 PM
@ can be used as an indicator for session related objects in query analyzer or stored procedure, and yes, can be used to declare a table variable...

malik641
03-17-2008, 10:22 AM
Thanks for the info Gibbs and Stan. I've also found out that the '@@' means a globally defined variable. Here is the site for more info (http://manuals.sybase.com/onlinebooks/group-as/asg1250e/sqlug/@Generic__BookTextView/41423;pt=40957#X).