CREATE VIEW V_RAND AS SELECT RAND1 = CONVERT(INT,RAND()*26),RAND2 = RAND()*2
GO
CREATE FUNCTION DBO.f_GetRandNum ( @LEN INT, @FLAG INT )
RETURNS NVARCHAR(100)
AS --@LEN 输出字符的长度
--@FLAG 返回值包含字符 1:大写字母 2:小写字母 3:大小写字母混合
BEGIN
DECLARE @SQL NVARCHAR(100) ,
@RAND INT
SELECT @SQL = ' '
IF @LEN > 100
SET @LEN = 100
WHILE @LEN > 0
BEGIN
SELECT @RAND = RAND1 + ( CASE @FLAG
WHEN 1 THEN 65
WHEN 2 THEN 97
ELSE ( CASE WHEN RAND2 > 1 THEN 97
ELSE 65
END )
END )
FROM V_RAND
SELECT @SQL = @SQL + CHAR(@RAND) ,
@LEN = @LEN - 1
END
RETURN @SQL
END
GO
--调用存储过程
SELECT DBO.f_GetRandNum(4, 3)+'@' +right(str(rand(),8,8),3)
GO
CREATE FUNCTION DBO.f_GetRandNum ( @LEN INT, @FLAG INT )
RETURNS NVARCHAR(100)
AS --@LEN 输出字符的长度
--@FLAG 返回值包含字符 1:大写字母 2:小写字母 3:大小写字母混合
BEGIN
DECLARE @SQL NVARCHAR(100) ,
@RAND INT
SELECT @SQL = ' '
IF @LEN > 100
SET @LEN = 100
WHILE @LEN > 0
BEGIN
SELECT @RAND = RAND1 + ( CASE @FLAG
WHEN 1 THEN 65
WHEN 2 THEN 97
ELSE ( CASE WHEN RAND2 > 1 THEN 97
ELSE 65
END )
END )
FROM V_RAND
SELECT @SQL = @SQL + CHAR(@RAND) ,
@LEN = @LEN - 1
END
RETURN @SQL
END
GO
--调用存储过程
SELECT DBO.f_GetRandNum(4, 3)+'@' +right(str(rand(),8,8),3)