Link to home
Start Free TrialLog in
Avatar of Endelm
Endelm

asked on

SQL Variable - Result as SomeName

I have this query:

Create Mytable
(somefield int)

DECLARE @Num numeric (10,10)

SELECT top 1 @Num = somefield
from...
where...

I always get (No Column Name) when I get my result.

SELECT top 1 @Num = somefield as SomeCoolName   // This doesn't work.
from...
where...

Any ideas?
ASKER CERTIFIED SOLUTION
Avatar of derekkromm
derekkromm
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ksaul
ksaul

When you specify a local variable(@Num) the value is set for the variable.  You can alias a field with similar syntax:

SELECT top 1 SomeCoolName  = YourField
from YourTable

OR another way to alias a field:

SELECT top 1 Field AS SomeCoolName  
from YourTable