Simple Stored Procedure with WHILE Loop - Code from Query Analyzer works

I have a SQL statement that works in query analyzer, but I am not sure how to save it as a stored procedure.  The error message (Incorrect syntax near the keyword 'while'.) occurs when I try to use the code attached.  The stored procedure will be executed by users of an ADP file so I have the user enter the variables for the beginning and ending ticket numbers.


CODE FROM QUERY ANALYZER - this code adds tickets 12000-12500 correctly with blank values.
declare @Start_Ticket_Number int
declare @End_Ticket_Number int
select @Start_Ticket_Number = 12000
select @End_Ticket_Number = 12500
while (@Start_Ticket_Number < @End_Ticket_Number)
BEGIN
select @Start_Ticket_Number = @Start_Ticket_Number +1
insert into piticket (TicketNum, PartNum, Description, Count, WipJobNum, WipOpNum, Locator, UOM) values (@Start_Ticket_Number, '', '', '' ,'', '', '', '')
END



CODE FROM STORED PROCEDURE
Create PROCEDURE Add_Tickets(@End_Ticket_Number int, @Start_Ticket_Number int)
AS
SELECT

while (@Start_Ticket_Number < @End_Ticket_Number)
BEGIN
select @Start_Ticket_Number = @Start_Ticket_Number +1
insert into piticket (TicketNum, PartNum, Description, Count, WipJobNum, WipOpNum, Locator, UOM) values (@Start_Ticket_Number, '', '', '' ,'', '', '', '')
END
LVL 4
gj99116Asked:
Who is Participating?
 
solution46Connect With a Mentor Commented:
gj99116,

the first SELECT after the AS is unnecessary and will be causing the problems.

s46.
0
 
gj99116Author Commented:
I have been staring at this problem so long (and made the error of copying/pasting) that I can't believe how simple this solution was.  Well it is very important that I have this SP finished ASAP so it is worth the points.

Thx solution46 for pointint out my error.
0
 
solution46Commented:
no probs ;)

s46.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.