0

I need some help here, I need to add records to a database table and this table only has 1 column which is an int called numbers and this table already has 300 records and I need to add 2300 more rows while executing the query.

I put together a query here but I am not sure it will work so I need someone to look at it and make sure my query looks good or if I am missing something here.

query to add 2300 rows

DECLARE             @Numbers AS INT
SET                 @Numbers = 301
WHILE  @Numbers <= 2300
BEGIN
    INSERT INTO dbo.Numbers VALUES(@Numbers)
    SET @Numbers=@Numbers+1
END
FunCoder
  • 147
  • 1
  • 2
  • 16

1 Answers1

1

Why not use recursive way ??

with t as (
     select 301 as Numbers 
     union all
     select Numbers+1
     from t
     where Numbers < 2300
)

insert into dbo.Numbers (Numbers)
select Numbers
from t
option (maxrecursion 0);

For your current WHILE loop, it should be start from 301 not 2300

So, it should be

SET @Numbers = 301
WHILE @Numbers <= 2300
. . . 
Yogesh Sharma
  • 49,870
  • 5
  • 26
  • 52
  • yes not sure why I didn't notice that, but I modified my first post, can you take a look if that would fix the issue please look above. – FunCoder Jun 04 '18 at 18:14
  • @AndresBryan29. . . Now you don't need to check first condition. – Yogesh Sharma Jun 04 '18 at 18:19
  • I removed the first condition please take a look. – FunCoder Jun 04 '18 at 18:21
  • thanks guys all your suggestions worked I was able to add the new 2300 rows to my table – FunCoder Jun 04 '18 at 18:38
  • why my questions is being mark as duplicate, even though my question description might look same as other questions but what I needed here is different results I could not find what I need it in other solutions. – FunCoder Jun 13 '18 at 16:01