Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using string values as column names

Status
Not open for further replies.

Bobbber

Programmer
Sep 6, 2002
83
TR
Hi,

This sounds like a strange question, but is there any function (or any other way) of passing a string in and getting out the field with the same name in a query?

Something like this (SalesTotals is a column in tblSalesTotal) :

SELECT someFunction ('SalesTotals') from database.schema.tblSalesTotal

The reason I ask is because I want to be able to dynamically select a field value based upon the string held in another table.

Thanks in advance,

Rob

 


You could rig something up using

sys.columns and sys.objects which would return tables that have column names matching a string then I guess you could create some Dynamic SQL from there to run your selects.



I love deadlines. I like the whooshing sound they make as they fly by
Douglas Adams
(1952-2001)
 
Yes, that will work, we have to do it frequently with Dynamic SQL here.

wb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top