Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RAND function

Status
Not open for further replies.

newtosql

Programmer
Joined
Apr 7, 2003
Messages
18
Location
US
Does anyone know what Im doing wrong? I get an error msg saying invalid use of RAND within a function. Thanks!


CREATE FUNCTION RandString
-- Input length of string to be returned
(@LenString int
)
RETURNS char(50)
AS
BEGIN
DECLARE @position int, @AtoZstring char(26), @ResultString char(50)
SET @position = 1
SET @AtoZstring='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
SET @ResultString=''
WHILE @position <= @LenString

BEGIN
SET @ResultString=@ResultString+SUBSTRING(@AtoZstring, round(1+rand()*26,0) 1)
SET @position = @position + 1
END
RETURN (@ResultString)
END
 
You cannot call non-deterministic functions (such as RAND) from the body of your own functions.

Look up CREATE FUNCTION in BOL for more info.

--James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top