I guess what's throwing me off is the second group of code, where a field name is SELECTed into a variable:
SELECT @var1 = CompanyName
FROM Customers
WHERE CustomerID = 'ALFKA'
Looking at the other code as well, apparently MS-SQL can run a SELECT command without actually touching a table, yes?
>The code example is demonstrating some of your options for assigning a value to @var1. The first example is a simple assignment, and could be accomplished just as easily with SET instead of SELECT.
>
>The second example shows how to assign a scalar value from a query, similar to what SqlCommand.ExecuteScalar() does in .NET.
>
>The third example is another simple assignment using 'AS' instead of '='.
>
>Once you get the hang of it, I think you will enjoy using the @ prefix. It is very useful when dealing with parameters to stored procedures. For instance, when you are passing values destined for columns in an UPDATE statement, it is convenient to use parameter names that exactly match the column names in the table. E.g. UPDATE Customers SET CompanyID = @CompanyID, CustomerName = @CustomerName, etc...
>
>>Now
that's easy to deduce.
>>
>>What's tough is this code sample from the docs:
>>
>>
>>USE Northwind
>>DECLARE @var1 nvarchar(30)
>>SELECT @var1 = 'Generic Name'
>>
>>SELECT @var1 = CompanyName
>>FROM Customers
>>WHERE CustomerID = 'ALFKA'
>>
>>SELECT @var1 AS 'Company Name'
>>
>>
>>What the heck is going on hereācan you figure it out?
>>
>>
>>
>>>>BTW, what's all this '@' stuff about, anyway? What does an '@' do? I'm looking at the docs for
@local_variable, but it's going way over
my head...
>>>>
>>>
>>>The @ is just a requirement for naming your local vars in T-sql. I suppose it distinguishes variables from field and table names.
>>>
>>>DECLARE @state char(2)
>>>SET @state = 'UT'