Solved

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

Posted on 2008-06-22
1
312 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 500 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 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