?
Solved

"select VARIABLE from table" - how to put the right syntax in this statement

Posted on 2008-06-22
1
Medium Priority
?
325 Views
Last Modified: 2012-08-14
In the typical select statement of SELECT FIELDVAR FROM TABLE, how do I pass a variable for FIELDVAR?
In otherwords, if FIELDVAR=FIELD88, I want the resulting statement to be exectuded as "SELECT FIELD88 FROM TABLE"
I will likely kick myself when I get the answer....
0
Comment
Question by:dbrennecke
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 60

Accepted Solution

by:
chapmandew earned 2000 total points
ID: 21842560
to do something like that, you'll need to use dynamic SQL:

declare @fieldname nvarchar(200)
declare @sql nvarchar(1000)

set @fieldname = 'field88'
set @sql = 'select ' + @fieldname + ' from tablename'

execute sp_executesql @sql
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

752 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