all groups > c# > march 2007 >
You're in the

c#

group:

What is the use of using an AS clause in a SQL query?


What is the use of using an AS clause in a SQL query? weird0
3/31/2007 7:51:44 PM
c#: What is the use of using an AS clause in a SQL query something like:

SELECT accno AS AccountNumber from Accounts

What difference does it make when i use the ExecuteScalar() function
and store it in a variable of the name of my own choice..... like:

string AccountName=cmd.ExecuteScalar();
Re: What is the use of using an AS clause in a SQL query? Arne_Vajhøj
3/31/2007 11:05:40 PM
[quoted text, click to view]

None in that case, because I believe ExecuteScalar fetched
values by index and not by name.

But if you were using ExecuteReader and fetched values
by name instead of by index, then it would be necessary.

Arne

Re: What is the use of using an AS clause in a SQL query? Scott M.
3/31/2007 11:33:58 PM
"AS" indicates that you want the field returned under an alias.

In your example, it means that the "accno" data should be returned under the
alias name "AccountNumber".



[quoted text, click to view]

Re: What is the use of using an AS clause in a SQL query? Mark Rae
4/1/2007 12:00:00 AM
[quoted text, click to view]

None in this particular case...

However, using AS to create an alias of a column (or even a table) can be
particularly useful when e.g. you are joining two tables which contain
fields of the same name...

If you had a Customers table and a Sales table both of which had a field
called RecordID (after first firing the designer!), you might do something
like this:

SELECT
c.RecordID AS CustomerID,
c.Name,
c.Address,
s.RecordID AS SaleID,
s.ItemID,
s.Amount
FROM
Customers c
INNER JOIN Sales s ON c.RecordID = s.CustomerID

AddThis Social Bookmark Button