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!

help with code snippet 1

Status
Not open for further replies.

zzzzzeke

Programmer
Mar 1, 2004
14
US
what exactly is this all doing?


declare @Total int, @counter int, @cost int, @PartNum int
declare myCursor Cursor
Local
for select PartNum, cost from Inventory
open myCursor

select @total = count(PartNum) from inventory

set @counter = 1
while @counter <= @total
Begin
fetch myCursor into @PartNum, @Cost
if @cost > 50
print 'PartNum' + rtrim(@PartNum)+ 'Cost is greater than 50'
else
print 'PartNum' + rtrim(@PartNum)+ 'Cost is less than 50'
set @counter=@counter + 1
End


close myCursor
Deallocate myCursor





thanks
~zzzzzeke
 
See comments in the code.
Code:
/*declare the variables*/
declare @Total int, @counter int, @cost int, @PartNum int
/*declare a cursor selecting the Part number and cost from the Inventory table.*/
declare myCursor Cursor
     Local
     for select PartNum, cost from Inventory 
/*Open the cursor that was just declared*/
open myCursor
/*Get the total number of parts*/
select @total = count(PartNum) from inventory

set @counter = 1
/*starting a loop that will run until the @counter is greater than or equal to @total*/
while @counter <= @total
Begin
     /*fetch a record from the cursor into the variables listed*/
     fetch myCursor into @PartNum, @Cost
     /*based on the cost of the part say if the cost is over or under $50.*/
     if @cost > 50
          print 'PartNum' + rtrim(@PartNum)+ 'Cost is greater than 50'
     else
          print 'PartNum' + rtrim(@PartNum)+ 'Cost is less than 50'
     /*increments the counter by 1*/
     set @counter=@counter + 1
End
/*closes the cursor*/
close myCursor
Deallocate myCursor

Denny

--Anything is possible. All it takes is a little research. (Me)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top