ALTER PROCEDURE [dbo].[Usp_randomnumber] ( @Len INT = 1,--Number of random digits@Rows INT = 1 --random number of pens) as BEGIN DECLARE @T as TABLE([Random Number] VARCHAR(MAX)) DECLARE @l INT = 1,@R INT = 1 while @R <= @Rows BEGIN DECLARE @RN varchar(MAX)= "' while @l <= @Len --randomly generate each number of digitsBEGIN SET @RN = @RN + CHAR(ROUND(RAND()* 9 + -,0)) SET @l = @l + 1 END --if the same random number is generated, it will not be storedIF not EXISTS(SELECT [Random Number] from @T WHERE [Random Number] = @RN) BEGIN INSERT into @T SELECT @RN --INSERT into @T ([Random number]) VALUES (@RN)SET @R = @R + 1 --the records produced a total of random numbers.SET @l = 1 --initializes the number of digits of the random number to 1 after each generation of a random numberEND END SELECT [Random Number] from @T END
Call:
EXECUTE [dbo]. [usp_randomnumber] 8,1
SQL Server generates random numbers