Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
bakerjon
Posting Yak Master
145 Posts |
Posted - 2007-03-01 : 13:55:30
|
| Hey all, I don't write a lot of UDFs, but I've run into an interesting problem with a developer who needs to do some math on a column in set based way. Seems like the best time for a fucntion! However, the process requires that he do some math in a couple of ways. If one errors, return the other value. I know I know, don't code so errors can happen. Any way...How do you ignore an arithmetic error in a UDF without aborting or exiting the execution of the function?Thanks in advance for the help!JonHolding it down on the engineering tip, y'all!http://www.sqljunkies.com/weblog/outerjoin |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
bakerjon
Posting Yak Master
145 Posts |
Posted - 2007-03-01 : 14:19:51
|
| Here is the code which resembles the actual process we are having issue with:alter Function [dbo].[foo] (@portfolio_id int)RETURNS DECIMAL(12,5) ASBEGIN declare @ret decimal(19,2)declare @ret2 decimal (19,2) set @ret = null select @ret = @portfolio_id / 0 select @ret2 = 2RETURN isnull(@ret, @ret2)END;Thanks again for all your helpJonHolding it down on the engineering tip, y'all!http://www.sqljunkies.com/weblog/outerjoin |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2007-03-01 : 15:58:19
|
| I assume that you really what to know how to disable errors. But, I would highly suggest that you code it such a fashion as to avoid generating an error. Do you have a better example? Your division by zero error is always going to happen, so I am confused as to the point of it.-Ryan |
 |
|
|
bakerjon
Posting Yak Master
145 Posts |
Posted - 2007-03-01 : 16:26:52
|
| This example is not a good one I agree..In my code I have a Power function in place of the dive by zero statement and its in a loop. For some iteration say iteration 19 the power function exceeds the numeric value and gives overflow error and terminates there. The code should ideally skip that statement if that happens and go to the following statement.Hope I did not confuse you more.Thanks again for your response....JonHolding it down on the engineering tip, y'all!http://www.sqljunkies.com/weblog/outerjoin |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2007-03-01 : 17:11:12
|
Perhaps there is a better way, but the only way I know how to do something like that in a UDF would be to check the value before your operation (assuming that is possible ij your case).For example, working with INTs and trying to avoid an overflow:IF (2147483647 - @CurrentValue) > @NextValue SET @ret = @CurrentValue + @NextValueELSE SET @ret = NULL Something like that, but it still sounds bad to me.. :) |
 |
|
|
|
|
|
|
|