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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Doing an incremental FOR...LOOP statement in T-SQL

Status
Not open for further replies.

jasonsalas

IS-IT--Management
Jun 20, 2001
480
GU
I'd like to run a FOR statement, such that it seeds a series of records with a value, and so that the logic resembles the following JavaScript/C# loop for 99 records:

for(int i=0;i<100;i++)
{
INSERT INTO Table (EmpID,Department) VALUES (i,'2')
}

This is a one-time use thing just to instantiate values in a new table, so I *could* get away with copying-and-pasting the T-SQL statement and manually incrementing the EmpID, but that would defeat the purpose of programming.

Is there a way to do this in T-SQL?
 
Code:
declare @v_i int
SET @v_i = 1
WHILE @v_i<=100
BEGIN
  --do stuff here
 INSERT INTO Table (EmpID,Department) VALUES (@v_i,'2')

  SET @v_i = @v_i + 1
END

"Own only what you can carry with you; know language, know countries, know people. Let your memory be your travel bag.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top