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

SQL loop

Status
Not open for further replies.

meckeard

Programmer
Aug 17, 2001
619
US
Hi Gang,

I need to do some quick and dirty inserts into a table.
What's the best way to do it in SQL w/o manually inserting each record?

Is there a loop statement that would work? Or do I need to use a cursor?

Thanks,
Mark
 
Whats the source of the rows you are inserting ? If you are selecting from another table you should be able to use something along the lines

INSERT INTO MyTable
SELECT field1,field2
FROM MyTable2
 
If you don't have a source and just want to insert some test data you can use WHILE to create a loop.

Example:

DECLARE
@nNrOfRows int,
@nRow int

SET @nNrOfRows = 100
SET @nRow = 0

WHILE(@nNrOfRows > @nRow)
BEGIN
INSERT INTO ...
SET @nRow = @nRow + 1
END
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top