If you need to insert random numbers mixed with strings into your table in SQL Server insert them one by one with few records, it’s ok. But if you need to insert many records, unique, and not duplicate numbers, it may be difficult for you.

But don’t worry the SQL Server can do it, you want to random numbers with strings and INSERT INTO a table 1000 or more recordings within a loop function. So you can do it with me step by step at the following :

Loop Function in SQL

The first time, please create a loop function. For example, loop 1000 records

DECLARE @i int = 0
WHILE @i < 1000 
BEGIN
    SET @i = @i + 1
    /* your code */
END

How to Random number with string with StoredProcedure

Create StoredProcedure for random numbers with strings together

Create proc [dbo].[RandomChars]
    @len int,
    @min tinyint = 48,
    @range tinyint = 74,
    @exclude varchar(50) = '0:;<=>?@O[]`^\/._',
    @output varchar(50) output
as 
    declare @char char
    set @output = ''
 
    while @len > 0 begin
       select @char = char(round(rand() * @range + @min, 0))
       if charindex(@char, @exclude) = 0 begin
           set @output += @char
           set @len = @len - 1
       end
    end

How to Call StoredProcedure

Call StoredProcedure and display random code in a list. For example, I need the length of the code to be 8

declare @newcode varchar(20)
exec [dbo].RandomChars @len=8, @output=@newcode out
Select (@newcode)

How to Insert Into a table

So you can mix code together of Loop function and random stored procedure :

DECLARE @i int = 0
WHILE @i < 1000
BEGIN
    SET @i = @i + 1
	declare @newcode varchar(20)
	exec [dbo].RandomChars @len=8, @output=@newcode out
        Insert into CODE_LISTS(CODE) VALUES(UPPER(@newpwd))
END

Final Result :

Copy the above code and run it on SQL Server, and you will get the result.