Solved

Function to Derive Field Names from Field Values - Sql Server

Posted on 2013-05-14
4
206 Views
Last Modified: 2013-05-15
Hi all,
I've been trying to write a function to derive values from a table that I need to use for a sql server.  The Parameter to pass in is a unique identifier - AttributeID and the field name will be AttributeName.  

AttributeID                                                       AttributeName
AABA0A56-1736-4822-99BE-02E33E1B1B57      Department
EB19A347-C50F-4440-BF3E-359FB5F95987      Address
BC1E774F-A36F-4FDD-BD1A-35B398DF7142      Last Name
61D62DCA-C8B9-4B84-B3A4-37560F8652F8      First Name
53372579-B97C-4901-9ED8-3791B3DA05BF      Date of Birth

Has anyone done something like this before?  I'm not sure where to begin.

Thanks,
Julia
0
Comment
Question by:Redheadache
4 Comments
 
LVL 21

Accepted Solution

by:
Dale Burrell earned 500 total points
ID: 39166859
If I understand you correctly, one of the two following options:

-- SVF - straight forward
create function dbo.Attribute_Name
(
  @Id varchar(38) -- or uniqueidentifier, varchar(38) will handle both int and guid
)
returns nvarchar(256) -- whatever is the max size of name
as
begin
  return (select Name from AttributeTable where id = @Id)
end

select dbo.Attribute_Name('123-123-123-12') -- Usage

-- OR

-- ITVF, often better performing, but slightly messier
create function dbo.Attribute_Name
(
  @Id varchar(38) -- or uniqueidentifier, varchar(38) will handle both int and guid
)
returns table
return (select Name from AttributeTable where id = @Id)

select (select top 1 Name from dbo.Attribute_Name('123-13-123-')) -- Usage

Open in new window

0
 
LVL 12

Expert Comment

by:funwithdotnet
ID: 39166883
Do you want to supply an ID and return the name?
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39166891
mmm, I smell a performance rat

Is there more than just that one table involved here? those sample records look to me like pointers to some non-normalized data in some other table(s).
0
 

Author Comment

by:Redheadache
ID: 39168054
That's correct, supply an ID and return the name.  Then I need to use the name returned as a field name in a select statement.  I think I use brackets or something for that but I'll test it this morning to figure it out.

It is non-normalized raw data  - that's correct.  This is a thing that I will develop properly in the future but for now I just need something quick and dirty that I can reuse until I have time to get this data into a star schema and put together an analytic.  

It's a start up company and there are five thousand different projects going on so every second I save is one I can spend sleeping.

I'll try the suggestion above.  Thanks much for the quick responses.

J
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

743 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

10 Experts available now in Live!

Get 1:1 Help Now