I am creating a SQL function (MS-SQL) to be used in a sproc to create random strings of a random length. When I write the entire function out, the Create Function line gets the red line of doom, claiming that "Incorrect syntax: Create Function must be the only statement in the batch."
Code is as follows:
Create Function dbo.randomtesttext (@length int)
returns varchar(999)
as
Begin
declare @pool varchar
set @pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
declare @return varchar(999)
declare @position int = 0
while (@position < @length)
begin
set @return = @return + (select substring(@pool, convert(int, rand()*36), 1))
set @position = @position + 1
end
return @return
end
Any thoughts? I'm sure the answer is a simple one that I'm just not experienced enough to see.
gos. However, you can't userandin a function.@positionso this will get you into an infinite loop, unless@lengthis 0.