Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Query Help

0 views
Skip to first unread message

Rick

unread,
Aug 5, 2009, 4:31:40 PM8/5/09
to
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?


Andrew J. Kelly

unread,
Aug 5, 2009, 8:12:49 PM8/5/09
to

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" <rfe...@newsgroups.nospam> wrote in message
news:%23cVX$ugFKH...@TK2MSFTNGP03.phx.gbl...

Erland Sommarskog

unread,
Aug 6, 2009, 2:37:22 AM8/6/09
to
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, esq...@sommarskog.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/prodinfo/previousversions/books.mspx

Bernd Maierhofer

unread,
Aug 6, 2009, 3:15:31 AM8/6/09
to

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" <rfe...@newsgroups.nospam> schrieb im Newsbeitrag
news:%23cVX$ugFKH...@TK2MSFTNGP03.phx.gbl...

0 new messages