Este é um exemplo de Store Procedure para criação de PIN/Password Aleatórios no SQL SERVER.
Experimentem:
- EXEC USP_CreateRandomPWD 0, 4 -> Exemplo para PIN 4 algarismos
- EXEC USP_CreateRandomPWD 1, 20 -> Exemplo para Password de 20 caracteres
A Store Procedure está no 1º Comentário...
1 comentário:
-- ====================
-- = Store Procedure: =
-- ====================
CREATE PROCEDURE [dbo].[USP_CreateRandomPWD]
(
@type as int = 0,
@maxSize as int = 4
)
AS
BEGIN
DECLARE @RandomValue AS INT
SET @RandomValue = 0
DECLARE @possibleValues AS NVARCHAR(10)
SET @possibleValues = 'A5K2E6M4X0'
IF(@type = 0)
BEGIN
IF(@maxSize >= 4 AND @maxSize < 10)
BEGIN
DECLARE @multiplifier AS NVARCHAR(250)
SET @multiplifier = '1'
WHILE LEN(@multiplifier) <= @maxSize
BEGIN
SET @multiplifier = @multiplifier + '0'
END
DECLARE @multiValue AS INT
SET @multiValue = CAST(@multiplifier AS INT)
SET @RandomValue = CAST((RAND() * @multiValue) AS INT)
WHILE LEN(@RandomValue) <> @maxSize
BEGIN
SET @RandomValue = CAST((RAND() * @multiValue) AS INT)
END
SELECT @RandomValue AS RandomPWD
END
ELSE
BEGIN
SELECT NULL AS RandomPWD
END
END
ELSE
BEGIN
IF(@type = 1)
BEGIN
IF(@maxSize <= 100)
BEGIN
DECLARE @returnPWD AS NVARCHAR(100)
SET @returnPWD = ''
WHILE LEN(@returnPWD) < @maxSize
BEGIN
SET @RandomValue = CAST((RAND() * 10) AS INT)
WHILE LEN(@RandomValue) <> 1
BEGIN
SET @RandomValue = CAST((RAND() * 10) AS INT)
END
SET @returnPWD = @returnPWD + RIGHT(LEFT(@possibleValues, @RandomValue+1), 1)
END
SELECT @returnPWD as RandomPWD
END
ELSE
BEGIN
SELECT NULL as RandomPWD
END
END
ELSE
BEGIN
SELECT NULL as RandomPWD
END
END
END
GO
Enviar um comentário