Hi nauna,
Refer below query.
SQL
--EXEC [dbo].[Random_Number_Generator] 5
CREATE PROCEDURE [dbo].[Random_Number_Generator]
@maxNumbers INT
AS
BEGIN
BEGIN TRY
DROP TABLE #rand_numbers
END TRY
BEGIN CATCH
PRINT '##rand_numbers not available to drop'
END CATCH
CREATE TABLE #rand_numbers
(
rand_number VARCHAR(6)
)
DECLARE @loop_ctr INT = 0
WHILE @loop_ctr < @maxNumbers
BEGIN
DECLARE @chars NCHAR(62)
SET @chars = N'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
DECLARE @AlphaNumeric NCHAR(5)
SET @AlphaNumeric = SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1)
+ SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1)
+ SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1)
+ SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1)
+ SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1)
INSERT #rand_numbers(rand_number)
SELECT @AlphaNumeric
SET @loop_ctr = @loop_ctr + 1
END
SELECT rand_number FROM #rand_numbers
END
Output
rand_number |
dX9VC |
E1Hl5 |
Rg2BC |
ABD9U |
VBQcO |