SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[FN_random_pwd](@len SMALLINT)
RETURNS VARCHAR(20) AS
/*
SELECT CHAR(65 + CEILING(RAND() * 25)) --随机字母(大写)-> myRand_big
SELECT CHAR(97 + CEILING(RAND() * 25)) --随机字母(小写)-> myRand_small
SELECT CAST(CEILING(RAND() * 9) AS VARCHAR(1)) --随机数字1至9的随机数字(整数)-> myRand_number
*/
BEGIN
DECLARE @i INT
DECLARE @flag INT
DECLARE @ra VARCHAR(12)
---最后会生成的随机码
--初始化设定
SET @i = 1
SET @ra = ''
--生成长度为@len设置的随机码
WHILE @i <= (@len)
BEGIN
--设置随机,这个随机会选择字母(大小写)还是数字
SELECT @flag = r FROM myRand_flag
IF @flag = 1
BEGIN
--随机字母(大写)
SELECT @ra = @ra + r
FROM myRand_big
END
ELSE
IF @flag = 2
BEGIN
--随机字母(小写)
SELECT @ra = @ra + r
FROM myRand_small
END
ELSE
BEGIN
--随机数字1至的随机数字(整数)
SELECT @ra = @ra + r
FROM myRand_number
END
SET @i = @i + 1
END
RETURN @ra
END
/*
SELECT dbo.fn_random_pwd(6) AS [PASSWORD] --密码位数
--生成随机密码后再根据密码生成其MD5码
SELECT UPPER(RIGHT(sys.fn_VarBinToHexStr(HASHBYTES('MD5',CAST([PASSWORD] AS VARCHAR(20)))),32))
*/
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[FN_random_pwd](@len SMALLINT)
RETURNS VARCHAR(20) AS
/*
SELECT CHAR(65 + CEILING(RAND() * 25)) --随机字母(大写)-> myRand_big
SELECT CHAR(97 + CEILING(RAND() * 25)) --随机字母(小写)-> myRand_small
SELECT CAST(CEILING(RAND() * 9) AS VARCHAR(1)) --随机数字1至9的随机数字(整数)-> myRand_number
*/
BEGIN
DECLARE @i INT
DECLARE @flag INT
DECLARE @ra VARCHAR(12)
---最后会生成的随机码
--初始化设定
SET @i = 1
SET @ra = ''
--生成长度为@len设置的随机码
WHILE @i <= (@len)
BEGIN
--设置随机,这个随机会选择字母(大小写)还是数字
SELECT @flag = r FROM myRand_flag
IF @flag = 1
BEGIN
--随机字母(大写)
SELECT @ra = @ra + r
FROM myRand_big
END
ELSE
IF @flag = 2
BEGIN
--随机字母(小写)
SELECT @ra = @ra + r
FROM myRand_small
END
ELSE
BEGIN
--随机数字1至的随机数字(整数)
SELECT @ra = @ra + r
FROM myRand_number
END
SET @i = @i + 1
END
RETURN @ra
END
/*
SELECT dbo.fn_random_pwd(6) AS [PASSWORD] --密码位数
--生成随机密码后再根据密码生成其MD5码
SELECT UPPER(RIGHT(sys.fn_VarBinToHexStr(HASHBYTES('MD5',CAST([PASSWORD] AS VARCHAR(20)))),32))
*/