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

How to update using a function

Take a table, where not all the columns are populated:

CREATE TABLE #T (A int, B int, C int, D int)
INSERT #T (A,B) VALUES (1,2)
INSERT #T (A,B) VALUES (3,4)
INSERT #T (A,B) VALUES (5,6)
INSERT #T (A,B) VALUES (7,8)
INSERT #T (A,B) VALUES (9,10)

The values for C and D can be computed as functions of A and B. For this
example, let's say they are twice A and three times B, respectively:

CREATE FUNCTION dbo.F(@A int,@B int)
RETURNS @Tbl TABLE (X int, Y int)
AS BEGIN
INSERT @Tbl (X,Y) VALUES (@A*2, @B*3)
RETURN
END

Now we use the function to compute the other columns:

UPDATE #T SET C=X, D=Y
FROM dbo.F(A,B)

Right? Well, no. Instead, I get this message:

Server: Msg 155, Level 15, State 1, Line 2
'A' is not a recognized OPTIMIZER LOCK HINTS option.

Any suggestions? I would like to use this structure, if possible.

Jim Geissman
Countrywide Home Loans
Jul 20 '05 #1
7 35039

"Jim Geissman" <ji**********@countrywide.com> wrote in message
news:b8**************************@posting.google.c om...
Take a table, where not all the columns are populated:

CREATE TABLE #T (A int, B int, C int, D int)
INSERT #T (A,B) VALUES (1,2)
INSERT #T (A,B) VALUES (3,4)
INSERT #T (A,B) VALUES (5,6)
INSERT #T (A,B) VALUES (7,8)
INSERT #T (A,B) VALUES (9,10)

The values for C and D can be computed as functions of A and B. For this
example, let's say they are twice A and three times B, respectively:

CREATE FUNCTION dbo.F(@A int,@B int)
RETURNS @Tbl TABLE (X int, Y int)
AS BEGIN
INSERT @Tbl (X,Y) VALUES (@A*2, @B*3)
RETURN
END

Now we use the function to compute the other columns:

UPDATE #T SET C=X, D=Y
FROM dbo.F(A,B)

Right? Well, no. Instead, I get this message:

Server: Msg 155, Level 15, State 1, Line 2
'A' is not a recognized OPTIMIZER LOCK HINTS option.

Any suggestions? I would like to use this structure, if possible.

Jim Geissman
Countrywide Home Loans


Unfortunately, this doesn't work since you can't use column names as
parameters in this situation. In this specific case, you could simply create
C and D as computed columns:

CREATE TABLE #T (A int, B int, C as A*2, D as B*3)

Alternatively, you could use scalar UDFs:

create function dbo.F2(@i int)
returns int
as
begin
return @i*2
end

create function dbo.F3(@i int)
returns int
as
begin
return @i*3
end

update #T set C=dbo.F2(A), D=dbo.F3(B)

But scalar functions perform badly on large data sets, because they are
invoked per-row, so this might not be appropriate. If neither of these
options are useful, then the simplest solution may just be a stored
procedure which does the UPDATE.

Simon
Jul 20 '05 #2
You need a scalar UDF instead of a table-valued UDF:

CREATE FUNCTION dbo.FA(@A INTEGER)
RETURNS INTEGER
AS
BEGIN
RETURN @A*2
END
GO

CREATE FUNCTION dbo.FB(@B INTEGER)
RETURNS INTEGER
AS
BEGIN
RETURN @B*3
END
GO

UPDATE #T
SET c=dbo.FA(a),
d=dbo.FB(b)

Don't store calculated results in the table if you can help it. If C and D
are always calculated from A and B then just drop the C and D columns and do
the calculation in a view or query instead.

--
David Portas
SQL Server MVP
--
Jul 20 '05 #3
> Thanks for the suggestions. Unfortunately, in the real case, the function
values are a property price index and its estimated reliability, which
are computed from relatively elaborate lookups and calculations based on
zip code, effective date and property type. The key point to me is that variable names can't be used as function
parameters in a case like this. Too bad.


I'm not sure I understand why that is a problem. UDF parameters are passed
by value not by reference and in this respect my function is no different
from the one you posted - I just used different names.

If you post your function code (also DDL and sample data INSERT statements)
maybe we can help you rewrite it exactly as required. Also consider Simon's
point about the performance overhead of a UDF. Possibly you can use an
UPDATE statement, query or view to achieve the same result without using a
function.

--
David Portas
SQL Server MVP
--
Jul 20 '05 #4
> Don't store calculated results in the table if you can help it. If C and D
are always calculated from A and B then just drop the C and D columns and do
the calculation in a view or query instead.


Thanks for the suggestions. Unfortunately, in the real case, the function
values are a property price index and its estimated reliability, which
are computed from relatively elaborate lookups and calculations based on
zip code, effective date and property type.

The key point to me is that variable names can't be used as function
parameters in a case like this. Too bad.

Jim
Jul 20 '05 #5
"David Portas" <RE****************************@acm.org> wrote in message news:<kP********************@giganews.com>...
Thanks for the suggestions. Unfortunately, in the real case, the function
values are a property price index and its estimated reliability, which
are computed from relatively elaborate lookups and calculations based on
zip code, effective date and property type.

The key point to me is that variable names can't be used as function
parameters in a case like this. Too bad.


I'm not sure I understand why that is a problem. UDF parameters are passed
by value not by reference and in this respect my function is no different
from the one you posted - I just used different names.

If you post your function code (also DDL and sample data INSERT statements)
maybe we can help you rewrite it exactly as required. Also consider Simon's
point about the performance overhead of a UDF. Possibly you can use an
UPDATE statement, query or view to achieve the same result without using a
function.


Sure, I can do this with two separate function calls, each of which returns
a single scalar value. However, computing value A and value B both visit a
lot of the same lookups and computations, so it seems to me there is
duplication of effort, hence lower than optimum efficiency. Because I'm
looking at doing this approx 60 million times, I would like to use that
work to compute both values at once. I would also like to use the elegance
of a function, which makes the code easier to read. Two function calls
preserve the elegance by hiding the details, and that's what I did last
time, and will do this time. I was simply hoping to combine all that work
and retrieve both results from one call.

I don't care about the extra time required to come up with the values. I
don't do the work -- a computer does it, and computers don't complain about
unnecessary work. I will simply adjust the schedule. But it still seems
odd that I can use the function approach to get values one at a time, but
not two at a time.

Jim
Jul 20 '05 #6
Jim Geissman (ji**********@countrywide.com) writes:
Sure, I can do this with two separate function calls, each of which
returns a single scalar value. However, computing value A and value B
both visit a lot of the same lookups and computations, so it seems to me
there is duplication of effort, hence lower than optimum efficiency.
Because I'm looking at doing this approx 60 million times, I would like
to use that work to compute both values at once. I would also like to
use the elegance of a function, which makes the code easier to read.
Two function calls preserve the elegance by hiding the details, and
that's what I did last time, and will do this time. I was simply hoping
to combine all that work and retrieve both results from one call.

I don't care about the extra time required to come up with the values.
I don't do the work -- a computer does it, and computers don't complain
about unnecessary work. I will simply adjust the schedule. But it
still seems odd that I can use the function approach to get values one
at a time, but not two at a time.


There is some new syntax in the upcoming version of SQL Server that
permits you to call a table-valued function and passing column values
to it. I have not studied it in detail, though.

Anyway, this is SQL Server, and you could do one of two things that is
better than having two UDFs:

1) Return the two values in one, as a string, and then split that
string in the UPDATE statement. Sure is not going to give you
nice code, but at least you are not doing the compuation twice.

2) Keep your table-valued function, and set up a cursor on the
table to update, and update one row at a time:
UPDATE tbl
SET col3 = f.x, col4, f.y
FROM tbl t
CROSS JOIN f(@col1, @col2) f
WHERE t.keycol = @keyval
This may sound horrendeously inneffecient, but if you call an
scalar UDF in a set-based statement, you get something cursor-like
behind the scenes anyway.

Then, of course, you could devise the computation in your function so
that it works on all values in the table at once, but that may prove to
be a real challenge.

--
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 #7
Thanks a lot, Erland!

Jim
Jul 20 '05 #8

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

Similar topics

7
by: Valiz | last post by:
Hi, I am updating system time with IRIG time source which sends irregular pulses as shown below 11000011111100111111111111111111....(1-IRIG present and 0-IRIG not present) I need to update...
0
by: Ferindo Middleton Jr | last post by:
I am trying to write a Perl Function for one of the databases I'm building a web application for. This function is triggered to occur BEFORE INSERT OR UPDATE. This function is complex in that it...
1
by: Mike | last post by:
In C, we can typedef pointer to functions, and therefore use function tables. But what's the advantage of using function table? Thanks, Mike
1
by: may | last post by:
Hi, I'm trying to update a SQLServer table via a call to a stored procedure using the DataSet.Update(dataset) function. What I'm not sure about is how does this dataset get interpreted on the...
4
by: Jim Hammond | last post by:
It would be udeful to be able to get the current on-screen values from a FormView that is databound to an ObjectDataSource by using a callback instead of a postback. For example: public void...
2
by: Newbie | last post by:
Could someone help: I have been searching the web trying to figure out a way to use function key's in my vb.net 2003 program (No luck!). What I would like to do is enable the user to press (for...
3
by: drsantosh82 | last post by:
Hi, I am trying to implement a callback routine using function pointers. Basically, I am trying to avoid tying my callback invoking member to a particular class. Let me explain my problem...
1
by: tekedge | last post by:
Hi, I have to do an update using a query which uses CTE . Could any body please tell me how I can do an update. I am unable to update if I replace the final select with an update one. Thanks...
2
by: nikhizumi | last post by:
advantages of using FUNCTION in C++
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.