?
Solved

SQL Variable - Result as SomeName

Posted on 2007-07-24
2
Medium Priority
?
175 Views
Last Modified: 2010-03-19
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?
0
Comment
Question by:Endelm
2 Comments
 
LVL 15

Accepted Solution

by:
derekkromm earned 2000 total points
ID: 19555972
When you set a variable in a select statement it won't show up in the resultset.

You can do this:

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

followed by

select @num SomeCoolName

and that should work fine
0
 
LVL 10

Expert Comment

by:ksaul
ID: 19556002
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
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question