[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 334
  • Last Modified:

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

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
dbrennecke
Asked:
dbrennecke
1 Solution
 
chapmandewCommented:
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now