score:3

Accepted answer
;with sequence as (
    select N = row_number() over (order by @@spid)
    from sys.all_columns c1, sys.all_columns c2
)
insert into [Table] (UserID, FirstName, Lastname)
select
    'UserID' + right('000000' + cast(N as varchar(10)), 6),
    'John' + right('000000' + cast(N as varchar(10)), 6),
    'Doe' + right('000000' + cast(N as varchar(10)), 6)
from sequence where N <= 300000

Read More

score:1

I've adapted a script found in this article:

DECLARE @RowCount INT
DECLARE @RowString VARCHAR(14)
DECLARE @First VARCHAR(14)
DECLARE @LAST VARCHAR(14)
DECLARE @ID VARCHAR(14)
SET @ID = 'UserID'
SET @First = 'John'
SET @Last = 'Doe'
SET @RowCount = 1
WHILE @RowCount < 300001
BEGIN
    SET @RowString = CAST(@RowCount AS VARCHAR(10))
    SET @RowString = REPLICATE('0', 6 - DATALENGTH(@RowString)) + @RowString
    INSERT INTO TestTableSize ( 
         UserID  
        ,FirstName  
        ,LastName  
        ) 
    VALUES  
        (@ID + @RowString  
        , @First + @RowString  
        , @Last + @RowString)  
    SET @RowCount = @RowCount + 1 
END

score:3

You could use the ROW_NUMBER function to generate different numbers like this:

SQL Fiddle

MS SQL Server 2008 Schema Setup:

CREATE TABLE dbo.users(
  Id INT IDENTITY(1,1) PRIMARY KEY CLUSTERED,
  user_id VARCHAR(20),
  first_name VARCHAR(20),
  last_name VARCHAR(20)
);

GO
DECLARE @NoOfRows INT = 7;

INSERT INTO dbo.users(user_id, first_name, last_name)
SELECT 'User_'+n, 'John_'+n, 'Doe_'+n
FROM(
SELECT REPLACE(STR(ROW_NUMBER()OVER(ORDER BY (SELECT NULL))),' ','0') n FROM(
select TOP(@NoOfRows) 1 x from sys.objects A,sys.objects B,sys.objects C,sys.objects D,sys.objects E,sys.objects F,sys.objects G
)X
)N

Query 1:

SELECT * FROM dbo.users

Results:

| ID |         USER_ID |      FIRST_NAME |      LAST_NAME |
-----------------------------------------------------------
|  1 | User_0000000001 | John_0000000001 | Doe_0000000001 |
|  2 | User_0000000002 | John_0000000002 | Doe_0000000002 |
|  3 | User_0000000003 | John_0000000003 | Doe_0000000003 |
|  4 | User_0000000004 | John_0000000004 | Doe_0000000004 |
|  5 | User_0000000005 | John_0000000005 | Doe_0000000005 |
|  6 | User_0000000006 | John_0000000006 | Doe_0000000006 |
|  7 | User_0000000007 | John_0000000007 | Doe_0000000007 |

Just change the @NoOfRows to 300000 to get the number of rows you are looking for.


More questions

More questions with similar tag