我需要在TSQL中创建一个新密码,该密码需要包含8个字符(a-z),其中必须包含上下字符以及至少一个数字。
任何人都可以帮我吗?
答案 0 :(得分:22)
select cast((Abs(Checksum(NewId()))%10) as varchar(1)) +
char(ascii('a')+(Abs(Checksum(NewId()))%25)) +
char(ascii('A')+(Abs(Checksum(NewId()))%25)) +
left(newid(),5)
from yourTable
答案 1 :(得分:5)
要考虑的事情,有点复杂:
SELECT TOP 8
SUBSTRING(tblSource.vsSource, tblValue.number + 1, 1) AS vsChar
FROM (SELECT 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' AS vsSource) AS tblSource
JOIN master..spt_values AS tblValue
ON tblValue.number < len(tblSource.vsSource)
WHERE tblValue.type = 'P'
ORDER BY NEWID()
但非常随意。
答案 2 :(得分:3)
这是一个为您提供非常强随机密码的选项。它使用numbers table但不是那么简单。
declare @chars char(62), @bytes binary(8)
set @chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
set @bytes = CRYPT_GEN_RANDOM(8)
select SUBSTRING(@chars, (SUBSTRING(@bytes, n.Number, 1) % LEN(@chars)) + 1, 1)
from dbo.Numbers n
where n.Number between 1 and LEN(@bytes)
FOR XML PATH ('')