home
training courses
why choose us?
solutions
support
company
LearnItFirst User Forum
Welcome Guest
Search
|
New Posts
|
Members
|
Log In
|
Register
SQL Server Forum - LearnItFirst.com
»
Transact-SQL Programming
»
Stored Procedures, Triggers, & Functions
»
Domain error when executing user defined function
Domain error when executing user defined function
Options
Previous Topic
·
Next Topic
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?
Back to top
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.
Back to top
Users browsing this topic
Guest
Forum Jump
SQL Server Database Administration
- General SQL Database Question & Answer
- Backup, Recovery and Disaster Recovery
- SQL Server Security
- Integration Services (SSIS) and DTS
Transact-SQL Programming
- DML (SELECT, INSERT, UPDATE, DELETE) Questions
- Stored Procedures, Triggers, & Functions
SQL Server Scripts, Code Samples and SSMS Custom Reports
- All SQL Server Versions
- SQL Server 2005/2008
- SQL Server Management Studio Custom Reports
Customer Service
- Video Requests
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.
SQL Server 2005 DBA Training Videos
SQL Server 2008 DBA Training Videos
Watch this topic
RSS Feed
Email this topic
Print this topic
Threaded
Normal