Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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 I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now