Solved

Generic GetValue function

Posted on 2014-09-23
8
205 Views
Last Modified: 2014-09-25
I have a table called tblOptions with lots of columns in. There is only one row.

If at all possible, I am looking to write a generic scalar-valued function that returns the value of one column in the above mentioned table. The column name will be passed into the function as a parameter.

I would like to avoid having to have a CASE statement in the function as this will require maintenance.

I am looking to use this function for column defaults when creating tables as I'd rather that approach than triggers.

Is this possible?
0
Comment
Question by:JedNebula
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
Comment Utility
You could use the CHOOSE function instead.
0
 
LVL 37

Expert Comment

by:Neil Russell
Comment Utility
Would be far more sensible to have a table with just 2 columns, first column holding the old column name as the Primary key and the second as the value. A very simple select statement then works AND you are future proof.

What you have is a very poor design.
0
 
LVL 1

Author Comment

by:JedNebula
Comment Utility
Thank you both.

Neilsr, what you have said makes perfect sense. I'm not sure why I didn't do things that way. It was a long time ago. The next project I start I definitely will change to that method - thank you.

I'm afraid that the (albeit nasty) setup I have in place currently, is too aged and too frequently used to be changed. I have not used the ChOOSE function before. I have had a quick look, but I'm not seeing how I might use that to provide a solution. Could you throw an example together for me?
0
 
LVL 24

Expert Comment

by:Phillip Burton
Comment Utility
CHOOSE(calculation that results in a value from 1 to 4 for example, Column-Result1, Column-Result2, Column-Result3, Column-Result4)
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

by:JedNebula
Comment Utility
Thinking about this further, I'm not sure either of your suggestions could be used as column defaults.

For example, I might have another table called tblSalesLine containing a column called VAT_Rate. On row insert, if I want the value to be the DefaultVATRate value from the tblOptions table, the column default has to be either a function OR a column/expression made up from tblSalesLine members.

I don't think you can simply use SELECT DefaultVATRate  FROM tblOptions as the default value. Neither could you use the CHOOSE function.

Unless I'm missing something?
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
Comment Utility
This would be easy to do using dynamic SQL ... but you can't use dynamic SQL in a function.

Therefore, I don't see any way to do this except to use a CASE statement with the associated maintenance when a column is added (are columns really added that often)?
0
 
LVL 1

Author Comment

by:JedNebula
Comment Utility
Exactly.
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
It'd be easy enough to write code to generate the function using all columns currently in the table.  And you could create the function with SCHEMABINDING, forcing you to adjust it when the table changed.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

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

16 Experts available now in Live!

Get 1:1 Help Now