Unix Technical Forum

Using alias in an expression

This is a discussion on Using alias in an expression within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi I tried to use the alias a1 for the column f1 like this, but it fails: create table ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 02:57 AM
Gunnar Vøyenli
 
Posts: n/a
Default Using alias in an expression

Hi
I tried to use the alias a1 for the column f1 like this, but it fails:

create table a (f1 int not null primary key)
insert into a (f1) values (2)

select f1 a1 from a where a1 = 2

Server: Msg 207, Level 16, State 3, Line 1
Invalid column name 'a1'.

In my real situation f1 is a "pretty complex" calculation, and a1 is an
alias of that.
I wanted to use the alias in the query instead of repeating the calculation
several times.

If this use of alias is not legal, then is there a way to get around this?

TIA
Gunnar Vøyenli
EDB-konsulent as
NORWAY


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 02:57 AM
David Portas
 
Posts: n/a
Default Re: Using alias in an expression

Put your aliased column in a derived table query.

SELECT a1
FROM
(SELECT f1 AS a1
FROM A) AS A
WHERE a1 = 2

--
David Portas
SQL Server MVP
--


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 02:58 AM
Gunnar Vøyenli
 
Posts: n/a
Default Re: Using alias in an expression

Thanks alot, David!
Now, its working fine!
-Gunnar
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@acm.org> wrote in message
news:FaydnXYDFZyNfDDdRVn-hw@giganews.com...
> Put your aliased column in a derived table query.
>
> SELECT a1
> FROM
> (SELECT f1 AS a1
> FROM A) AS A
> WHERE a1 = 2
>
> --
> David Portas
> SQL Server MVP
> --
>
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:01 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com