Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

insert range of numbers

Status
Not open for further replies.

treyhunsucker

Programmer
May 16, 2006
60
US
Hello,

I can't seem to find any documentation on this.

I would like to insert a range of numbers.

Example: 1 thru 50

Instead of doing:

insert into table set (data) values ('1');
insert into table set (data) values ('2');
insert into table set (data) values ('3');

and so on I would like to be able to:

insert into table set (data) values ('1' thru '50');

I am sure it's possible I just can't find the right syntax

Thank you in advance
 
You can insert more than one item at a time like this:

Code:
insert into yourtable (data)
values
(1),(2),(3),(4),(5)

but a faster way if you are looking to do this for a higher number of rows, or figure you will use it going forward is the following.

create this table

Code:
create table integers(i tinyint);
insert into integers values
(0),(1),(2),(3),(4),(5),(6),(7),(8),(9);

you only have to create that table a single time. Now anytime you need to fill a row with values you can use a cross join on that table like this:

Code:
insert into yourtable(data)

select
10*tens.i + units i
from integers as tens
cross join
integers as units
where
10*tens.i + units i between 1 and 50
 
Thank you for your response, that answers my question.

I will give it a test and see how it works for me :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top