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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 rand() function error

Author  Topic 

mpandey3
Starting Member

1 Post

Posted - 2009-03-01 : 19:45:20
CREATE FUNCTION ufntGenerateRandValue
(
@minValue int,
@maxValue int
)
RETURNS int
AS
BEGIN
DECLARE @randNumber int

Set @randNumber = Cast(RAND() * @maxValue + @minValue AS INT)
RETURN @randNumber;

END
GO

This function compiles without error but when I tried to run it I got the following errors:
Invalid use of side-effecting or time-dependent operator in 'rand' within a function.

Please help me figure it out
Thank you.

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2009-03-01 : 20:37:27
You can't use RAND() in a UDF. See http://weblogs.sqlteam.com/jeffs/archive/2004/11/22/2927.aspx for workarounds.
Go to Top of Page
   

- Advertisement -