473,396 Members | 1,996 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.

UDF and persisted column

Hi All,

I am trying to create a user defined function that fetches the price of
an item. I have written a scalar function that takes itemid and returns
its price.
Simple version of tables would be like this:

Item(
itemid int,--pk
price decimal(18,4)
)

item_detail(
redid int , --pk
itemid int, --fk
cust_id int,--fk
order_qty decimal(18,4)
)

Now I want to create a computed persisted column having formula

dbo.GetPrice(Itemid) * order_qty

I get the error cannot be persisted because column is
non-deterministic.

function is as following:

create function GetPrice(@itemid int)
Returns decimal(18,4)
AS
Begin
declare @price decimal(18,4)

select @price =price from Item where itemid=@itemid

return @price
End
Go

I would appreciate if someone can shed some light that why this
function is considered non-deterministic by SQL Server 2005. Is there
any work around this behaviour?
Thanks in advance for all your remarks.
NH

Jan 22 '07 #1
5 5552

SQLMan_25 wrote:
Hi All,

I am trying to create a user defined function that fetches the price of
an item. I have written a scalar function that takes itemid and returns
its price.
Simple version of tables would be like this:

Item(
itemid int,--pk
price decimal(18,4)
)

item_detail(
redid int , --pk
itemid int, --fk
cust_id int,--fk
order_qty decimal(18,4)
)

Now I want to create a computed persisted column having formula

dbo.GetPrice(Itemid) * order_qty

I get the error cannot be persisted because column is
non-deterministic.

function is as following:

create function GetPrice(@itemid int)
Returns decimal(18,4)
AS
Begin
declare @price decimal(18,4)

select @price =price from Item where itemid=@itemid

return @price
End
Go

I would appreciate if someone can shed some light that why this
function is considered non-deterministic by SQL Server 2005. Is there
any work around this behaviour?
Thanks in advance for all your remarks.
NH
You don't want to do things like this.
Suppose SQL Server let you store your computed value. Suppose later
somebody changed the relevant Item.price. Now you have a stale value in
your persisted column and you do not know about it.
Why can't you just select?

-----------------------
Alex Kuznetsov
http://sqlserver-tips.blogspot.com/
http://sqlserver-puzzles.blogspot.com/

Jan 22 '07 #2
Alex,
Thank you for your reply. Computed persisted colum is a business need.
I want when price of one item changes, it should be reflected in
computed column. Otherwise I have do these updates via jobs and
calculate this value. I want to persist so I can build index on it etc
etc.

Regards,
NH
Alex Kuznetsov wrote:
SQLMan_25 wrote:
Hi All,

I am trying to create a user defined function that fetches the price of
an item. I have written a scalar function that takes itemid and returns
its price.
Simple version of tables would be like this:

Item(
itemid int,--pk
price decimal(18,4)
)

item_detail(
redid int , --pk
itemid int, --fk
cust_id int,--fk
order_qty decimal(18,4)
)

Now I want to create a computed persisted column having formula

dbo.GetPrice(Itemid) * order_qty

I get the error cannot be persisted because column is
non-deterministic.

function is as following:

create function GetPrice(@itemid int)
Returns decimal(18,4)
AS
Begin
declare @price decimal(18,4)

select @price =price from Item where itemid=@itemid

return @price
End
Go

I would appreciate if someone can shed some light that why this
function is considered non-deterministic by SQL Server 2005. Is there
any work around this behaviour?
Thanks in advance for all your remarks.
NH

You don't want to do things like this.
Suppose SQL Server let you store your computed value. Suppose later
somebody changed the relevant Item.price. Now you have a stale value in
your persisted column and you do not know about it.
Why can't you just select?

-----------------------
Alex Kuznetsov
http://sqlserver-tips.blogspot.com/
http://sqlserver-puzzles.blogspot.com/
Jan 22 '07 #3

SQLMan_25 wrote:
Alex,
Thank you for your reply. Computed persisted colum is a business need.
I want when price of one item changes, it should be reflected in
computed column. Otherwise I have do these updates via jobs and
calculate this value. I want to persist so I can build index on it etc
etc.

Regards,
NH
Alex Kuznetsov wrote:
SQLMan_25 wrote:
Hi All,
>
I am trying to create a user defined function that fetches the price of
an item. I have written a scalar function that takes itemid and returns
its price.
Simple version of tables would be like this:
>
Item(
itemid int,--pk
price decimal(18,4)
)
>
item_detail(
redid int , --pk
itemid int, --fk
cust_id int,--fk
order_qty decimal(18,4)
)
>
Now I want to create a computed persisted column having formula
>
dbo.GetPrice(Itemid) * order_qty
>
I get the error cannot be persisted because column is
non-deterministic.
>
function is as following:
>
create function GetPrice(@itemid int)
Returns decimal(18,4)
AS
Begin
declare @price decimal(18,4)
>
select @price =price from Item where itemid=@itemid
>
return @price
End
Go
>
I would appreciate if someone can shed some light that why this
function is considered non-deterministic by SQL Server 2005. Is there
any work around this behaviour?
Thanks in advance for all your remarks.
NH
You don't want to do things like this.
Suppose SQL Server let you store your computed value. Suppose later
somebody changed the relevant Item.price. Now you have a stale value in
your persisted column and you do not know about it.
Why can't you just select?

-----------------------
Alex Kuznetsov
http://sqlserver-tips.blogspot.com/
http://sqlserver-puzzles.blogspot.com/

Under most circumstances I would just join these two tables. having an
index on (itemID, price), the join should not add too much overhead.
If, however, you really want to denormalize and store price in the
child table, you can use ON UPDATE CASCADE to do all dirty work for
you:

CREATE TABLE Items(ItemID INT NOT NULL PRIMARY KEY,
Price DECIMAL(10,2),
CONSTRAINT UK_Items UNIQUE(ItemID, Price)
)
go
CREATE TABLE OrderDetail(ItemID INT NOT NULL,
Qty INT NOT NULL,
Price DECIMAL(10,2),
CONSTRAINT FK_OrderDetail_Items FOREIGN KEY(ItemID, Price) REFERENCES
Items(ItemID, Price) ON UPDATE CASCADE
)
go
SET NOCOUNT ON
INSERT Items VALUES(1, 24.95)
INSERT Items VALUES(2, 2.99)
INSERT OrderDetail VALUES(1, 5, 24.95)
INSERT OrderDetail VALUES(2, 3, 2.99)
SELECT * FROM OrderDetail
/*
ItemID Qty Price
----------- ----------- ------------
1 5 24.95
2 3 2.99
*/
UPDATE Items SET Price = 19.95 WHERE ItemID = 1
SELECT * FROM OrderDetail
/*
ItemID Qty Price
----------- ----------- ------------
1 5 19.95
2 3 2.99
*/

-----------------------
Alex Kuznetsov
http://sqlserver-tips.blogspot.com/
http://sqlserver-puzzles.blogspot.com/

Jan 22 '07 #4
Alex,
once again thanks for your prompt reply. I will consider your
suggestion. There are always more than one way to skin the cat..lol. I
have other solutions for the same issue. I only need this info for
display purposes.
My original question was why my function is considered
non-deterministic. Any ideas, articles, or links?

Thanks,
NH
Alex Kuznetsov wrote:
SQLMan_25 wrote:
Alex,
Thank you for your reply. Computed persisted colum is a business need.
I want when price of one item changes, it should be reflected in
computed column. Otherwise I have do these updates via jobs and
calculate this value. I want to persist so I can build index on it etc
etc.

Regards,
NH
Alex Kuznetsov wrote:
SQLMan_25 wrote:
Hi All,

I am trying to create a user defined function that fetches the price of
an item. I have written a scalar function that takes itemid and returns
its price.
Simple version of tables would be like this:

Item(
itemid int,--pk
price decimal(18,4)
)

item_detail(
redid int , --pk
itemid int, --fk
cust_id int,--fk
order_qty decimal(18,4)
)

Now I want to create a computed persisted column having formula

dbo.GetPrice(Itemid) * order_qty

I get the error cannot be persisted because column is
non-deterministic.

function is as following:

create function GetPrice(@itemid int)
Returns decimal(18,4)
AS
Begin
declare @price decimal(18,4)

select @price =price from Item where itemid=@itemid

return @price
End
Go

I would appreciate if someone can shed some light that why this
function is considered non-deterministic by SQL Server 2005. Is there
any work around this behaviour?
Thanks in advance for all your remarks.
NH
>
You don't want to do things like this.
Suppose SQL Server let you store your computed value. Suppose later
somebody changed the relevant Item.price. Now you have a stale value in
your persisted column and you do not know about it.
Why can't you just select?
>
-----------------------
Alex Kuznetsov
http://sqlserver-tips.blogspot.com/
http://sqlserver-puzzles.blogspot.com/


Under most circumstances I would just join these two tables. having an
index on (itemID, price), the join should not add too much overhead.
If, however, you really want to denormalize and store price in the
child table, you can use ON UPDATE CASCADE to do all dirty work for
you:

CREATE TABLE Items(ItemID INT NOT NULL PRIMARY KEY,
Price DECIMAL(10,2),
CONSTRAINT UK_Items UNIQUE(ItemID, Price)
)
go
CREATE TABLE OrderDetail(ItemID INT NOT NULL,
Qty INT NOT NULL,
Price DECIMAL(10,2),
CONSTRAINT FK_OrderDetail_Items FOREIGN KEY(ItemID, Price) REFERENCES
Items(ItemID, Price) ON UPDATE CASCADE
)
go
SET NOCOUNT ON
INSERT Items VALUES(1, 24.95)
INSERT Items VALUES(2, 2.99)
INSERT OrderDetail VALUES(1, 5, 24.95)
INSERT OrderDetail VALUES(2, 3, 2.99)
SELECT * FROM OrderDetail
/*
ItemID Qty Price
----------- ----------- ------------
1 5 24.95
2 3 2.99
*/
UPDATE Items SET Price = 19.95 WHERE ItemID = 1
SELECT * FROM OrderDetail
/*
ItemID Qty Price
----------- ----------- ------------
1 5 19.95
2 3 2.99
*/

-----------------------
Alex Kuznetsov
http://sqlserver-tips.blogspot.com/
http://sqlserver-puzzles.blogspot.com/
Jan 22 '07 #5

SQLMan_25 wrote:
Alex,
once again thanks for your prompt reply. I will consider your
suggestion. There are always more than one way to skin the cat..lol. I
have other solutions for the same issue. I only need this info for
display purposes.
My original question was why my function is considered
non-deterministic. Any ideas, articles, or links?

Thanks,
NH
Alex Kuznetsov wrote:
SQLMan_25 wrote:
Alex,
Thank you for your reply. Computed persisted colum is a business need.
I want when price of one item changes, it should be reflected in
computed column. Otherwise I have do these updates via jobs and
calculate this value. I want to persist so I can build index on it etc
etc.
>
Regards,
NH
Alex Kuznetsov wrote:
SQLMan_25 wrote:
Hi All,
>
I am trying to create a user defined function that fetches the price of
an item. I have written a scalar function that takes itemid and returns
its price.
Simple version of tables would be like this:
>
Item(
itemid int,--pk
price decimal(18,4)
)
>
item_detail(
redid int , --pk
itemid int, --fk
cust_id int,--fk
order_qty decimal(18,4)
)
>
Now I want to create a computed persisted column having formula
>
dbo.GetPrice(Itemid) * order_qty
>
I get the error cannot be persisted because column is
non-deterministic.
>
function is as following:
>
create function GetPrice(@itemid int)
Returns decimal(18,4)
AS
Begin
declare @price decimal(18,4)
>
select @price =price from Item where itemid=@itemid
>
return @price
End
Go
>
I would appreciate if someone can shed some light that why this
function is considered non-deterministic by SQL Server 2005. Is there
any work around this behaviour?
Thanks in advance for all your remarks.
NH

You don't want to do things like this.
Suppose SQL Server let you store your computed value. Suppose later
somebody changed the relevant Item.price. Now you have a stale value in
your persisted column and you do not know about it.
Why can't you just select?

-----------------------
Alex Kuznetsov
http://sqlserver-tips.blogspot.com/
http://sqlserver-puzzles.blogspot.com/

Under most circumstances I would just join these two tables. having an
index on (itemID, price), the join should not add too much overhead.
If, however, you really want to denormalize and store price in the
child table, you can use ON UPDATE CASCADE to do all dirty work for
you:

CREATE TABLE Items(ItemID INT NOT NULL PRIMARY KEY,
Price DECIMAL(10,2),
CONSTRAINT UK_Items UNIQUE(ItemID, Price)
)
go
CREATE TABLE OrderDetail(ItemID INT NOT NULL,
Qty INT NOT NULL,
Price DECIMAL(10,2),
CONSTRAINT FK_OrderDetail_Items FOREIGN KEY(ItemID, Price) REFERENCES
Items(ItemID, Price) ON UPDATE CASCADE
)
go
SET NOCOUNT ON
INSERT Items VALUES(1, 24.95)
INSERT Items VALUES(2, 2.99)
INSERT OrderDetail VALUES(1, 5, 24.95)
INSERT OrderDetail VALUES(2, 3, 2.99)
SELECT * FROM OrderDetail
/*
ItemID Qty Price
----------- ----------- ------------
1 5 24.95
2 3 2.99
*/
UPDATE Items SET Price = 19.95 WHERE ItemID = 1
SELECT * FROM OrderDetail
/*
ItemID Qty Price
----------- ----------- ------------
1 5 19.95
2 3 2.99
*/

-----------------------
Alex Kuznetsov
http://sqlserver-tips.blogspot.com/
http://sqlserver-puzzles.blogspot.com/
It is non-deterministic by definition - which means that you cannot
guarantee that it will always return the same result for the same input
parameter.

-----------------------
Alex Kuznetsov
http://sqlserver-tips.blogspot.com/
http://sqlserver-puzzles.blogspot.com/

Jan 22 '07 #6

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

Similar topics

5
by: nimdez | last post by:
Hi, I am working on an existing code base in which a lot of data displayed to the user is formatted in tables. Most tables are printed row-by-row using printf() with "%s" print conversion...
4
by: Bill | last post by:
I have a DataSet that is serialized to a local disk. One of the testers recently experienced a problem where data was saved and the Tablet PC she was testing later went offline. When she restarted...
4
by: perspolis | last post by:
I have 3 columns in my datatabel name Total,unit,Price. I use a column expression in my project..and in this expression i multiplied two column... for example ...
0
by: Toby Mills | last post by:
I have <DIV> control, which has its style.display setting controlled by client-side vbscript. I have set runat = server, but the setting is not persisted when the webform reloads. I can persist...
0
by: Tom Jorgenson | last post by:
Can anyone give me sample code for creating a persisted (between runs) cookie that holds the user name and password? Would appreciate it if you could be specific, since I've been fighting this for...
8
by: Anthony P. Mancini | last post by:
I'm working on a proof of concept that will ultimately be deployed on a load balancer. For the sake of a preliminary demonstration I created a C# object and marked it's attributes as Public...
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
0
by: marfi95 | last post by:
Basically, I keep the state of some objects serialized in an xml file through the use of a data set. The xml file serialized is persisted onto a shared network drive. However, the file is only...
3
by: PJ6 | last post by:
Taken out of context I know this may seem like a strange thing to do, but just take this as an academic question if necessary... Calling methods on a persisted control that has been added to a...
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: 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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.