Link to home
Start Free TrialLog in
Avatar of gj99116
gj99116

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of solution46
solution46

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gj99116
gj99116

ASKER

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.
no probs ;)

s46.