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

PROPER SYNTAX FOR LOOPING SQL STATEMENT 2

Status
Not open for further replies.

leishad

Technical User
Apr 3, 2003
88
US
I do not know the right syntax to achieve my desired result:

This would be my beginning point:

INSERT INTO table_job (job_no)
VALUES (variablenumber)

But I need to add the verbage to loop through this 1,000 times (adding 1,000 records) incrementing the variablenumber each time. What is appropriate in Query Analyzer?
 
try
Code:
declare @i int
set @i = 1
while @i <= 1000
begin
INSERT INTO table_job (job_no) values(@i)
set @i = @i + 1
end
select * from table_job

Have you looked at Identity fields?

"I'm living so far beyond my income that we may almost be said to be living apart
 
declare i int
set i = 1

while i < 1001
Begin
INSERT INTO table_job (job_no)
VALUES (i)

set i = i + 1
end

Thanks,
Patrick
 
Thank-you both, however, first I noted that it looked like you both did exactly the same thing. Then I pasted into analyzer and found that PLGREEN solution gave errors while just by adding the "@" symbol in front of the "i" does the trick. So my question to hmckillop is "why is that?" and a comment - I will look up info on identity fields.

Thanks
 
i is a variable and needs to be declared with @. I left that out on accident.
 
leishad,

What is the purpose of your original post. Do you plan to just add 1 to 1000 in your database and that is it? Or is there more to it?
If you let us know if you are doing something more then maybe we can give further help, if this is all you plan to do then glad to have been of assistance.

Thanks


"I'm living so far beyond my income that we may almost be said to be living apart
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top