473,385 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Function Parameter

I am writing a function which will take two parameters. One the field
to be returned from a table and second parameter is the ID of the
record to be returned.

Problem is it's not returning the value of the field specified in the
parameter but instead returns the parameter itself. Is there a
function that will get the parameter to be evaluted first?

ALTER FUNCTION [dbo].[getScholarYearData]
(
-- Add the parameters for the function here
@FieldName varchar(50), @ScholarID int
)
RETURNS varchar(255)
AS
BEGIN
-- Declare the return variable here
DECLARE @ResultVar varchar(255)

-- Add the T-SQL statements to compute the return value here
SELECT @ResultVar=EXECUTE(@FieldName)
FROM dbo.qmaxScholarYearID INNER JOIN
dbo.tblScholarYears ON
dbo.qmaxScholarYearID.ScholarID = dbo.tblScholarYears.ScholarID AND
dbo.qmaxScholarYearID.MaxOfScholarYearID =
dbo.tblScholarYears.ScholarYearID

-- Return the result of the function
RETURN @ResultVar

END

Apr 19 '07 #1
10 4157

Hi,

I would do it using a procedure as follows:

CREATE PROCEDURE GetFieldValue
@strTableName VARCHAR(250),
@strFieldName VARCHAR(250),
@nID DECIMAL(9,0),
@strResult VARCHAR(250) OUTPUT
AS
BEGIN
DECLARE @strSQL NVARCHAR(4000)

SET @strSQL = 'SELECT @strResult = ' + @strFieldName
+ ' FROM ' + @strTableName
+ ' WHERE ID = ' + CAST (@nID AS VARCHAR(9))

EXEC sp_executesql @strSQL,
N'@strResult VARCHAR(250) OUTPUT',
@strResult OUTPUT
END
GO

And then execute it in a way like this:

DECLARE
@strValue VARCHAR(250)

BEGIN

EXEC dbo.GetFieldValue 'TABLE1', 'COLUMN1', 1, @strValue OUTPUT

PRINT @strValue
END
GO

Obviously the print is just to ensure that you have the correct value.
This may not be the best way, but it's a way that works for me.

Hope this helps,

Paul

Apr 20 '07 #2
SQL Server (al*********@gmail.com) writes:
I am writing a function which will take two parameters. One the field
to be returned from a table and second parameter is the ID of the
record to be returned.

Problem is it's not returning the value of the field specified in the
parameter but instead returns the parameter itself. Is there a
function that will get the parameter to be evaluted first?

ALTER FUNCTION [dbo].[getScholarYearData]
(
-- Add the parameters for the function here
@FieldName varchar(50), @ScholarID int
)
RETURNS varchar(255)
AS
BEGIN
-- Declare the return variable here
DECLARE @ResultVar varchar(255)

-- Add the T-SQL statements to compute the return value here
SELECT @ResultVar=EXECUTE(@FieldName)
This does not even compile.

The fact that you want to pass a parameter for the column name indicates
that you have a poor table design. Columns should represent unique
attributes, and normally it's not meaningful to sometimes return one
column and sometimes another.

Nevertheless, you can use the CASE expression:

@ResultVar = CASE @FieldName
WHEN 'thiscol' THEN thiscol
WHEN 'thatcol' THEN thatcol
WHEN 'thatothercol' THEN thatothercol
END

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Apr 21 '07 #3
>I am writing a function which will take two parameters. One the field [sic] to be returned from a table and second parameter is the ID of the record [sic] to be returned. <<
You got nothing right in this code. Erland is nicer about it, but
that is real message.

1) fields and records are nothing like columns and rows. That is
basic SQL.

2) You have not looked at ISO-11179 rules for data elements. At least
get rid of that silly "tbl-" and learn why even Microsoft has dropped
camelCase. And things like a "year_id" make no sense; Years are
already unique entities and have a name; they do not need an id. You
might give them a name, like the Chinese Zodiac, but that is
reaching. But a year in the context of a student is an attribute for
attending school, graduation, etc.

Also, never put the data type in a data element name like Paul did;
This is SQL and not 1960's BASIC for a 16KB PC.

3) The whole idea of what you are trying to do is a violation of
coupling and cohesion -- remember them in your first software
engineering class? You should name this nightmare something like
"Britney Spears, Squids or Automobiles" so that people will know that
you have no idea what it does until run time.

Think for a second about how uselessly vague the name
"getScholarYearData" is. Which data?

4) If you REALLY want to learn, first get some books and courses --
you really need that SE course before any SQL or RDBMS course.

Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications. It is very hard to debug code when you do not let us
see it.

A bad design leads to these insane and impossible to maintain kludges
you are being offered to get you out of the way. It is easier than
trying to fill in an IT education in a posting.

Apr 22 '07 #4
Also, never put the data type in a data element name like Paul did;
This is SQL and not 1960's BASIC for a 16KB PC.
Please can you explain what you mean by this? This procedure works
fine for me, but If there is a better way of doing something then I
would like to know what it is. I am not sure we actually need to have
a procedure like this in our system, but this is how I would have
written it if so. Any improvements would be appreciated!

Apr 23 '07 #5
On Apr 23, 6:04 am, paul.wr...@gmail.com wrote:
Also, never put the data type in a data element name like Paul did;
This is SQL and not 1960's BASIC for a 16KB PC.

Please can you explain what you mean by this? This procedure works
fine for me, but If there is a better way of doing something then I
would like to know what it is. I am not sure we actually need to have
a procedure like this in our system, but this is how I would have
written it if so. Any improvements would be appreciated!

Don't waste your time with him. He's too busy insulting people to
offer any help. This stored procedure will be used to display
information in a form. Your code worked fine and I appreciate your
help.

In the end I decided to use an SQL statement and a subform, but was
just curious as to how I could manipulate the behavior of that
parameter. For the hate mongers please excuse my trying to learn
anything new.

I love the stuff about my table structure being poorly designed. They
know this without even seeing it. Or the naming conventions
changing. Oh sorry this database is about 10yrs old so I should go in
and start changing all the table names to make these big shots
happy.

Please if you don't have any useful information to share just move
along and keep your hateful statements to yourself.

Apr 28 '07 #6
SQL Server wrote:
I love the stuff about my table structure being poorly designed. They
know this without even seeing it.
It is not necessary to see your DDL to know much about the design.

Joe and Erland tried to do you a favor. Others like me just shook
their heads and just let it going knowing there are so many people
out there that think because they can write some syntactically
correct code they are developers.

You owe several people apologies.
--
Daniel A. Morgan
University of Washington
da******@x.washington.edu
(replace x with u to respond)
Apr 28 '07 #7
On Apr 28, 2:20 pm, DA Morgan <damor...@psoug.orgwrote:
SQL Server wrote:
I love the stuff about my table structure being poorly designed. They
know this without even seeing it.

It is not necessary to see your DDL to know much about the design.

Joe and Erland tried to do you a favor. Others like me just shook
their heads and just let it going knowing there are so many people
out there that think because they can write some syntactically
correct code they are developers.

You owe several people apologies.
--
Daniel A. Morgan
University of Washington
damor...@x.washington.edu
(replace x with u to respond)

I can take Erland's suggestion to rethink why I am doing what I am
doing (which I had already done) because it was delivered without
malice. Joe on the other hand is just plain nasty. I can do without
his kind of help.
Apr 29 '07 #8
>Please can you explain what you mean by this? <<

The ISO-11179 standards for naming data elements are based on the idea
that you name things for what they inherently are. That name is then
used everywhere in the schema.

You do NOT name them for:

1) How they are physically stored -- that means you do not put the
data type into the name. We had to do in the original versions of
BASIC because the interpreters needed that information to allocate
storage on the fly. A lot of programmers never un-learned that.

Physical locators generated by the physical storage are never
attributes in the schema. IDENTITY is never a key. We do not make
the user navigate the tables using track and sector numbers, etc. The
SQL engine is supposed to handle surrogates and not the humans.

It also means no silly "tb-" or "tbl-" to tell us it is a table
(there is only *one* data structure in SQL, duh!). And no vw-" affix
to tell us it is a VIEW (the "vw-" thing always looked like a
"Volkswagen" to me).

2) Where they are located - that means the table name is not part of
the data element name. Do you change your name from place to place as
you move around? Of course not. This practice also screws up the
data dictionary (if you do not have a data dictionary your project is
really screwed).

3) How they are used in one place -- that means no "pk-" or "fk-"
affixes. That is also silly because the same identifier that is a
FOREIGN KEY in the referencing has to be UNIQUE or a PRIMARY KEY in a
second table by definition.

The correct format is "<entity name>_<attribute type>" in lower case
for column names. The attribute types are defined in your data
dictionary, but I have a short list in other postings.

4) A data element name does not have multiple attribute types. That
means you can have "customer_id" or "customer_type" but never
"customer_type_id" because the attribute has to be either an
identifier (unique per customer) or a type (applies to many
customers).

This is usually a newbie confusing data and metadata in his attempt at
a data model.

5) A data element name is not a single attribute type. There is no
such thing as the magical, universal "id" or "date" or "value" etc.
An attribute has to be the identifier of something in particular, the
date of a particular kind of event, the value of a known attribute as
measured on a scale, etc. It is also a sign the project has no data
dictionary because you would quickly see that these magical vague
attributes apply to automobiles, squids and Britney Spears.

"To be is to be something in particular; to be nothing in particular
or everything in general is to be nothing." - Aristotle

6) A data element name is not a dangling entity name. The data
element name "customer" by itself begs the question "what?"
--"customer_id", "customer_type", "customer_name", or what? My
favorite is assuming that "sex" means "sex-frequency" or
"sex_preference" and not "sex_code" when I get a form.

The programmer has confused a table with a file and expects context to
provide the information he was too lazy to put into the table. The
field names in a file are local to the file; the column names are
global to the schema or better yet, are global to a data model that
covers your entire enterprise or industry.

An exception to this is the use of industry standard names that are
well understood in your enterprise. For example, VIN for automobiles,
ISBN for books, etc.

7) Tables are sets and should have collective or plural names, not
singular ones. That is, "Employee" is a bad table name (exception:
you really do have only one employee); "Employee" is better;
"Personnel" is best. Collective nouns imply a set by their nature and
will not be used in attribute names, which have a singular name
because their values apply to an element in the set.

An exception to this is the use of industry standard names that are
well understood in your enterprise. But most of these will be
collective nouns.

8) Relationship tables should use a common name for the relationship
and not an invented hyphenate. For example, "Roster" and not
"StudentClass" or worse.

I you wish, I can also post a quick look at ISO-11179.

Apr 29 '07 #9
--CELKO-- wrote:
If you wish, I can also post a quick look at ISO-11179.
You should even though the OP probably thinks reading and
standards are not part of the requirement for the job.

Others would enjoy reading it.
--
Daniel A. Morgan
University of Washington
da******@x.washington.edu
(replace x with u to respond)
Puget Sound Oracle Users Group
www.psoug.org
Apr 29 '07 #10
I you wish, I can also post a quick look at ISO-11179.
I'm trying to fight my way through the kludge of data online to get an
overview of this myself - and I happened to come across this thread.

Yes, a quick look at ISO-11179 would be great (an indept look would be
even better ;) )... I've inherated the tblConjoinedName legacy, but
have a chance with a new ap to do things right - if only I knew what
"right" was!

May 1 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
14
by: dover | last post by:
/*Copy the line a token at a time into the output*/ copy(istream_iterator<string>(iss), istream_iterator<string>(), ostream_iterator<string>(oss, " ")); What's the meaning of this statement?...
4
by: Vish | last post by:
Hi all, I am having a build error in one of the overloaded functions in my class. The function takes either a string as a parameter or a type referenced in another dll as a parameter. My class...
16
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the...
4
by: Tony Lownds | last post by:
(Note: PEPs in the 3xxx number range are intended for Python 3000) PEP: 3107 Title: Function Annotations Version: $Revision: 53169 $ Last-Modified: $Date: 2006-12-27 20:59:16 -0800 (Wed, 27 Dec...
10
by: Janus | last post by:
Hi, Is there a way to pass arguments to the callback function used inside an addEventListener? I see that I can only list the name of the callback function. For eg, I use this: var...
40
by: Angus | last post by:
Hello I am writing a library which will write data to a user defined callback function. The function the user of my library will supply is: int (*callbackfunction)(const char*); In my...
5
by: pauldepstein | last post by:
Hi all, I saw some code like this: unsigned short SomeFunc(unsigned short SomeNum, bool SomeBool, const SomeClass& SomeMem, bool(SomeClass::*AmemberFunctionOfSomeClass)(const unsigned...
1
by: Jorge | last post by:
On Oct 23, 10:36 am, Tuxedo <tux...@mailinator.comwrote: Yes: function first_function (PARAMETER) { preload_image = new Image(800,600); preload_image.onload = function () {...
10
by: Constantine AI | last post by:
Hi i am having a little problem with an equation function that was created from all your help previously. The function works fine itself but with a small glitch within it. Here is the function...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.