473,396 Members | 1,789 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,396 software developers and data experts.

Scalar Function Columns

Is it ill-advised to have columns whose values pull from scalar functions
using other fields in the record as parameters? For example, if I have

create table a(iID int primary key)
create table b(
iID int ,iDetail int,
CONSTRAINT PK PRIMARY KEY(iID,iDetail),
CONSTRAINT FK FOREIGN KEY (iID) REFERENCES a(iID)
)
Let's say in table b I put price information for each detail and in table a
I'd like to put a column that sums these prices for the children of each
record. Should I make a computed column that references a function using
iID as a parameter? Or would it be better to create a view for this kind of
purpose?
Regards,
Tyler
Jul 20 '05 #1
4 2107
Use a view. It's better to avoid dependent columns where possible because of
the work involved in keeping them up to date. The view will likely
outperform a computed column UDF doing the same job.

--
David Portas
SQL Server MVP
--
Jul 20 '05 #2
There's also a hidden gotcha with computed columns and INDEX, should that
arise ...
you start to need SCHEMA_BINDING, which has a ripple effect
in causing stored procedures to recompile, which adds to locking conflicts
....
yadda yadda.

"David Portas" <RE****************************@acm.org> wrote in message
news:Mb********************@giganews.com...
Use a view. It's better to avoid dependent columns where possible because of the work involved in keeping them up to date. The view will likely
outperform a computed column UDF doing the same job.

--
David Portas
SQL Server MVP
--

Jul 20 '05 #3
>> Is it ill-advised to have columns whose values pull from scalar
functions using other fields [sic] in the record [sic] as parameters
[sic]? <<

What are you talking about?? Rows are not records; fields are not
columns; tables are not files; parameters are used by functions and
procedures, not tables.
For example, if I have .. <<
Read ISO-11179 so you will stop prefixing data elements with their
storage type; it makes your code look, read And maintain like 1960's
BASIC OR 1950's FORTRAN II. I know this has nothing to do with your
question, but it is so fundamentally wrong I have to correct your bad
habit.

CREATE TABLE A
(a_id INTEGER NOT NULL PRIMARY KEY);

CREATE TABLE B
(a_id INTEGER NOT NULL REFERENCES A(a_id),
detail INTEGER NOT NULL);
Let's say in table B I put price information for each detail and in

table A I'd like to put a column that sums these prices for the
children of each
record [sic]. <<

Then you would use a VIEW and you'd have a price column *somewhere* in
the schema. What you posted was awful, even for a sample schema
skeleton.

Why would anyone even consider a proprietary, non-relational thing in
SQL? Because if you were in a file system, which does have fields and
records, you would write procedural code to solve the problem! YOu
have a lot to un-learn.
Jul 20 '05 #4
Tyler Hudson (Ty****@Spam.MeNOTallpax.com) writes:
Is it ill-advised to have columns whose values pull from scalar functions
using other fields in the record as parameters? For example, if I have

create table a(iID int primary key)
create table b(
iID int ,iDetail int,
CONSTRAINT PK PRIMARY KEY(iID,iDetail),
CONSTRAINT FK FOREIGN KEY (iID) REFERENCES a(iID)
)
Let's say in table b I put price information for each detail and in
table a I'd like to put a column that sums these prices for the children
of each record. Should I make a computed column that references a
function using iID as a parameter? Or would it be better to create a
view for this kind of purpose?


A view would be better, because if you say

SELECT iID, totprice FROM a

and totprice is a computed column with a UDF, the SELECT statement
is likely to be serialized as if it was a cursor with disastrous
effects on performance.

This does not happen if you make it a view.

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

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #5

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

Similar topics

2
by: Mountain Man | last post by:
Hi, I have an array derived from a set of radio buttons that I want to break down into a single variable for use with a database. How can I do this? $gender is the array, and I want $gender2 to...
7
by: Steve Jorgensen | last post by:
Hi all, I've been using scalar functions as a way to perform some complex data transformation operations, and I've noticed that scalar functions reaaaaalllllyyyy sloooowwwwww thiiiiiings...
7
by: roger | last post by:
I'm having difficulties invoking a user defined table function, when passing to it a parameter that is the result of another user defined function. My functions are defined like so: drop...
2
by: Martin MacRobert | last post by:
Hi, I'm trying to make a specialisation of a template function, so that the second parameter accepts scalar types only (int,double,float etc.). How can I do this without writing an explicit...
1
by: Rama Jayapal | last post by:
the following is my code can anybody rectify my problem that i get when running my application "Must declare scalar variable @ID" <asp:GridView ID="GridView1" DataKeyNames="ID" runat="server"...
0
by: roamnet | last post by:
hi i created database file with .mdf extention ,sql server as a source and use grid view to display data there're no problem in data retrieve and display,but i want to edit it or insert new...
2
by: mcleana | last post by:
I have a view that contains a complex query. A few of the columns call a function that returns a specific output. I also use a function to do a join as well. For example: SELECT l.ID,...
0
by: uymqlp502 | last post by:
Version 1.4 of my scalar class is available at http://RussP.us/scalar.htm No major changes. I have corrected the "repr" function to make it more useful, and I have added a "unit_type" function...
1
by: Mark | last post by:
I am really struggling with this, trying to understand why it doesn't work. I've simplified the code down to the essentials. The GridView displays in ReadOnly mode just fine, Clicking the "edit"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.