LearnItFirst User Forum

New SQL Server 2008 DBA Course
Welcome Guest Search | New Posts | Members | Log In | Register

Domain error when executing user defined function Options
tash
Posted: Friday, August 01, 2008 2:44:12 PM
Rank: Newbie

Joined: 7/28/2008
Posts: 2
Points: 6
I have this sql query (my User defined function, it takes string parameter and returns a table with calculated value for this string parameter, it uses 2 tables to get values for calculation and creates one temporary table "DFreqTable" to store temporary needed values):

Code:

use AR;
go
CREATE FUNCTION EntFunction
(@ItemName varchar(100))
returns @EntTable table (
        EffName varchar(100) NOT NULL,
        EntValue float NOT NULL)
as

BEGIN
DECLARE @TransactionNumber int;
DECLARE @DFreqTable table(
    DName varchar(100) NOT NULL,
    DFreq int NOT NULL);
SELECT @TransactionNumber = (SELECT count(TransactionID) FROM AR.dbo.EffectsTransactions WHERE EffectName=@ItemName)

INSERT INTO @DFreqTable
SELECT
        dr.DName,
        count(dr.DName)/@TransactionNumber AS DFreq
            FROM
                AR.dbo.DTransactions dr, AR.dbo.EffectsTransactions ef

WHERE
        ef.EffectName=@ItemName
        AND dr.TransactionID=ef.TransactionID
GROUP BY dr.DName
INSERT INTO @EntTable
SELECT @ItemName, sum(DFreq*LOG(DFreq)) FROM @DFreqTable
RETURN
END


I executed it as sql query and obtained new dbo object in my Object explorer (in Programmability->Functions)
Now I create new simple query:
Code:

select EntValue from AR.dbo.EntFunction('ABASIA');

and I get this error instead of float value of "EntValue" field after execution:
Code:

"A domain error occurred."

it is first UDF, I've written.
What is it? What I do wrong?
Scott Whigham
Posted: Friday, August 01, 2008 3:51:07 PM


Rank: Super Mod

Joined: 3/20/2006
Posts: 460
Points: 1,002
Where do you live?: Dallas, TX
It's your use of LOG(). Try this:
Code:
SELECT LOG(0)
Your "DFreq" is 0 in your SUM(DFreq*LOG(Dreq)) statement.

I think it's because you are doing integer-based math and thinking you are getting percents. Your statement "count(dr.DName)/@TransactionNumber AS DFreq" will only ever return an integer. "count(dr.DName)/CAST(@TransactionNumber AS FLOAT) AS DFreq" will return a float which would help you not get that error.
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.