SQL Server generates random numbers

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.