dbTalk Databases Forums  

Query Help

microsoft.public.sqlserver.clients microsoft.public.sqlserver.clients


Discuss Query Help in the microsoft.public.sqlserver.clients forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Rick
 
Posts: n/a

Default Query Help - 08-05-2009 , 04:31 PM






We have a function that basically returns a column name(mapping table), I'm
trying to run a query that will call that function to get the column name
and return a value from a field using that column name:

Function will return a value of 'ColumnName'
Select Field1 AS [dbo].GetDisplayName('Parm1','Parm2')
from [TABLENAMEHERE]

Should return:

ColumnName
-----------------
Field1
Field1

Can anyone tell me if this is possible or a better way to do this?

Reply With Quote
  #2  
Old   
Andrew J. Kelly
 
Posts: n/a

Default Re: Query Help - 08-05-2009 , 08:12 PM






Well there is really only 1 way to handle dynamic mapping if you can't avoid
that and that is with dynamic sql. Have a look here and make sure you are
aware of all the pros & cons he points out.
http://www.sommarskog.se/dynamic_sql.html


--
Andrew J. Kelly SQL MVP
Solid Quality Mentors


"Rick" <rfemmer (AT) newsgroups (DOT) nospam> wrote

Quote:
We have a function that basically returns a column name(mapping table),
I'm trying to run a query that will call that function to get the column
name and return a value from a field using that column name:

Function will return a value of 'ColumnName'
Select Field1 AS [dbo].GetDisplayName('Parm1','Parm2')
from [TABLENAMEHERE]

Should return:

ColumnName
-----------------
Field1
Field1

Can anyone tell me if this is possible or a better way to do this?

Reply With Quote
  #3  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Query Help - 08-06-2009 , 02:37 AM



Rick (rfemmer (AT) newsgroups (DOT) nospam) writes:
Quote:
We have a function that basically returns a column name(mapping table),
I'm trying to run a query that will call that function to get the column
name and return a value from a field using that column name:

Function will return a value of 'ColumnName'
Select Field1 AS [dbo].GetDisplayName('Parm1','Parm2')
from [TABLENAMEHERE]

Should return:

ColumnName
-----------------
Field1
Field1

Can anyone tell me if this is possible or a better way to do this?
Better way? Well, I don't know what is going on, but I get a feeling
that there is something in the database design that is not quite right.

As Andy says, you will end up with dynamic SQL, unless all possible
columns have the same data type, in which case you could use a CASE
expression.



--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

Reply With Quote
  #4  
Old   
Bernd Maierhofer
 
Posts: n/a

Default Re: Query Help - 08-06-2009 , 03:15 AM



AFAICS you are trying get another (more readable) header/fieldname instead
of the original fieldīs name. But I did not get it to work this way, I think
after the AS only literals are allowed.

As Andrew mentioned, have a look at dynamic sql.

Good luck, Bernd


"Rick" <rfemmer (AT) newsgroups (DOT) nospam> schrieb im Newsbeitrag
news:%23cVX$ugFKHA.5956 (AT) TK2MSFTNGP03 (DOT) phx.gbl...
Quote:
We have a function that basically returns a column name(mapping table),
I'm trying to run a query that will call that function to get the column
name and return a value from a field using that column name:

Function will return a value of 'ColumnName'
Select Field1 AS [dbo].GetDisplayName('Parm1','Parm2')
from [TABLENAMEHERE]

Should return:

ColumnName
-----------------
Field1
Field1

Can anyone tell me if this is possible or a better way to do this?

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.