我需要在函数中生成一些随机 nvarchar。所以我创建了这个函数:
CREATE FUNCTION [dbo].[sfp_GenerateRandomString]
(@length int) -- Length of string
RETURNS NVARCHAR
AS
BEGIN
DECLARE @random NVARCHAR(25) = ''
while @length > 0
begin
set @random = @random + nchar((cast(rand()*1000 as int)%26)+97)
set @length = @length-1
end
return @random
END
但它不起作用,因为在必须是 DETERMINISTIC 的 SQL 函数中禁止使用 RAND() 函数。并且 RAND() 是不确定的。
所以我的问题是在这种情况下如何生成随机数?谢谢。