Unix Technical Forum

Scalar Function Columns

This is a discussion on Scalar Function Columns within the SQL Server forums, part of the Microsoft SQL Server category; --> Is it ill-advised to have columns whose values pull from scalar functions using other fields in the record as ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 03:38 AM
Tyler Hudson
 
Posts: n/a
Default 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


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 03:38 AM
David Portas
 
Posts: n/a
Default Re: Scalar Function Columns

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
--


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 03:38 AM
Mischa Sandberg
 
Posts: n/a
Default Re: Scalar Function Columns

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" <REMOVE_BEFORE_REPLYING_dportas@acm.org> wrote in message
news:MbqdnQopMeXwu2XdRVn-tw@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
> --
>
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 03:39 AM
--CELKO--
 
Posts: n/a
Default Re: Scalar Function Columns

>> 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-29-2008, 03:39 AM
Erland Sommarskog
 
Posts: n/a
Default Re: Scalar Function Columns

Tyler Hudson (TylerH@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, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 12:49 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com