Solved

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

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

760 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

19 Experts available now in Live!

Get 1:1 Help Now