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

W

weird0

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();
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

weird0 said:
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();

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
 
S

Scott M.

"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".
 
M

Mark Rae

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();

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top