Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 FOR NEXT INSERT

Author  Topic 

sfjtraps
Yak Posting Veteran

65 Posts

Posted - 2009-09-17 : 11:21:08
Can someone explain how to INSERT multiple records, incrementing a field in the record. (Like doing a for next next type loop in VB)

For instance, if I want to INSERT the following data record 10 times and increment x from 1 to 10 each time a record is inserted. x being the first column.

x,2ndColumn,3rdColumn,4thColumn

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-09-17 : 11:26:12
You can do that with
select
row_number() over (order by (select 1)) as x,
2ndCol,
...
from ...

or
while begin end

or
insert table
select 1, 2ndCol,... union all
select 2, 2ndCol,... union all
select 3, 2ndCol,...


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -