Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to set default value for field?
Message
 
To
10/08/2003 14:42:51
Keith Payne
Technical Marketing Solutions
Florida, United States
General information
Forum:
ASP.NET
Category:
Databases
Miscellaneous
Thread ID:
00818645
Message ID:
00818767
Views:
23
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'

Read about the greatest fraud in the history of mankind.
See TaxableIncome.net.
Previous
Reply
Map
View

Click here to load this message in the networking platform