Yevgeny Simkin
Yevgeny Simkin

Reputation: 28349

How do I add several rows to a table in one insert, tracking the count as I go?

Without writing a stored procedure loop, is it possible to insert into a table 100 times setting some column's value to whatever iteration it's at within the "loop" (1-100).

Upvotes: 1

Views: 53

Answers (1)

Marc B
Marc B

Reputation: 360672

Cheap hack:

select @val := 1;

insert into yourtable (valfield) select (@val := @val + 1) from any_existing_table where @val < 100;

[edit] Mark, I edited your answer so that I could mark it as correct. -Dr.D [end edit]

Upvotes: 1

Related Questions